Spring Boot整合mybatis(一)实例代码
sprig-boot是一个微服务架构,加快了spring工程快速开发,以及简便了配置。接下来开始spring-boot与mybatis的整合。
1、创建一个maven工程命名为spring-boot-entity,pom.xml文件配置如下:
<project xmlns="http://maven.apache.org/pom/4.0.0" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelversion>4.0.0</modelversion> <groupid>com.spring.boot.entity</groupid> <artifactid>spring-boot-entity</artifactid> <packaging>jar</packaging> <version>0.0.1-snapshot</version> <distributionmanagement> <repository> <id>releases</id> <name>nexus release repository</name> <url>http://localhost:8081/nexus/content/repositories/releases/</url> </repository> <snapshotrepository> <id>snapshots</id> <name>nexus snapshot repository</name> <url>http://localhost:8081/nexus/content/repositories/snapshots/</url> </snapshotrepository> </distributionmanagement> <!-- 其中distributionmanagement节点配置的仓库为当前工程打包时发布的仓库 --> </project> 然后创建一个包,命名为com.spring.boot.entity,在该包下创建一个user.java文件,内容如下: /** * copyright (c) windliven 2016 all rights reserved * * @author liyj * @date 2017年7月11日 下午2:34:32 * @since v1.0.0 */ package com.spring.boot.entity; /** * todo * * @author liyj * @date 2017年7月11日 下午2:34:32 * */ public class userentity { /** * id */ private string id; /** * name */ private string name; /** * pass */ private string pass; /** * email */ private string email; /** * iphone */ private string iphone; public userentity() {} public userentity(string id) {this.id = id;} public string getid() { return id; } public void setid(string id) { this.id = id; } public string getname() { return name; } public void setname(string name) { this.name = name; } public string getpass() { return pass; } public void setpass(string pass) { this.pass = pass; } public string getemail() { return email; } public void setemail(string email) { this.email = email; } public string getiphone() { return iphone; } public void setiphone(string iphone) { this.iphone = iphone; } }
到此,spring-boot-entity工程创建完成。
2、创建一个maven工程,命名为spring-boot-interface,pom.xml文件配置如下:
<project xmlns="http://maven.apache.org/pom/4.0.0" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelversion>4.0.0</modelversion> <groupid>com.spring.boot.inter</groupid> <artifactid>spring-boot-interface</artifactid> <version>0.0.1-snapshot</version> <dependencies> <!-- 引入spring-boo-entity工程打成的jar包 --> <dependency> <groupid>com.spring.boot.entity</groupid> <artifactid>spring-boot-entity</artifactid> <version>0.0.1-snapshot</version> </dependency> </dependencies> <distributionmanagement> <repository> <id>releases</id> <name>nexus release repository</name> <url>http://localhost:8081/nexus/content/repositories/releases/</url> </repository> <snapshotrepository> <id>snapshots</id> <name>nexus snapshot repository</name> <url>http://localhost:8081/nexus/content/repositories/snapshots/</url> </snapshotrepository> </distributionmanagement> <!-- 其中distributionmanagement节点配置的仓库为当前工程打包时发布的仓库 --> </project> 然后创建一个包,命名为com.spring.boot.inter.service,在该包下创建userservice.java接口类。内容如下: /** * copyright (c) windliven 2016 all rights reserved * * @author liyj * @date 2017年7月11日 下午2:31:20 * @since v1.0.0 */ package com.spring.boot.inter.service; import java.util.list; import com.spring.boot.entity.userentity; /** * todo * * @author liyj * @date 2017年7月11日 下午2:31:20 * */ public interface userservice { /* * insert */ void insert(userentity entity); /* * deleteentity */ void deleteentity(userentity entity); /* * deletebyid */ void deletebyid(string id); /* * updateentity */ void updateentity(userentity entity); /* * updatebyid */ void updatebyid(string id); /* * getone */ userentity getone(string id); /* * getlist */ list<userentity> getlist(); }
到此,spring-boot-interface工程完成。
3、创建一个maven工程,命名为spring-boot-main,pom.xml文件内容如下:
<project xmlns="http://maven.apache.org/pom/4.0.0" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelversion>4.0.0</modelversion> <groupid>com.spring.boot.service</groupid> <artifactid>spring-boot-service</artifactid> <version>0.0.1-snapshot</version> <dependencies> <!-- entity、接口的jar包 --> <dependency> <groupid>com.spring.boot.entity</groupid> <artifactid>spring-boot-entity</artifactid> <version>0.0.1-snapshot</version> </dependency> <dependency> <groupid>com.spring.boot.inter</groupid> <artifactid>spring-boot-interface</artifactid> <version>0.0.1-snapshot</version> </dependency> <!-- spring-boot --> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot</artifactid> <version>1.4.1.release</version> </dependency> <!-- <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-maven-plugin</artifactid> <version>1.4.1.release</version> </dependency> --> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-test</artifactid> <version>1.4.1.release</version> </dependency> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter</artifactid> <version>1.4.1.release</version> </dependency> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-web</artifactid> <version>1.4.1.release</version> </dependency> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-beans</artifactid> <version>4.3.1.release</version> </dependency> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-test</artifactid> <version>4.3.1.release</version> </dependency> <!-- end --> <!-- mybatis --> <dependency> <groupid>org.mybatis.spring.boot</groupid> <artifactid>mybatis-spring-boot-starter</artifactid> <version>1.1.1</version> </dependency> <dependency> <groupid>org.mybatis</groupid> <artifactid>mybatis-spring</artifactid> <version>1.2.2</version> </dependency> <dependency> <groupid>org.mybatis</groupid> <artifactid>mybatis</artifactid> <version>3.2.6</version> </dependency> <!-- <dependency> <groupid>org.apache.ibatis</groupid> <artifactid>ibatis-sqlmap</artifactid> <version>2.3.0</version> </dependency> --> <!-- <dependency> <groupid>org.apache.ibatis</groupid> <artifactid>ibatis-core</artifactid> <version>3.0</version> </dependency> --> <!-- end --> <!-- 配置mysql驱动 --> <dependency> <groupid>mysql</groupid> <artifactid>mysql-connector-java</artifactid> <version>5.1.17</version> </dependency> <!-- end --> <!-- 配置c3p0数据源 --> <dependency> <groupid>com.mchange</groupid> <artifactid>c3p0</artifactid> <version>0.9.2.1</version> </dependency> <!-- end --> <!--util --> <dependency> <groupid>org.apache.commons</groupid> <artifactid>commons-lang3</artifactid> <version>3.4</version> </dependency> <!-- 添加缓存支持 --> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-cache</artifactid> <version>1.4.1.release</version> </dependency> <!-- 使用ehcache缓存方案 --> <dependency> <groupid>net.sf.ehcache</groupid> <artifactid>ehcache</artifactid> <version>2.9.0</version> </dependency> <!-- redis缓存 --> <dependency> <groupid>redis.clients</groupid> <artifactid>jedis</artifactid> <version>2.8.2</version> </dependency> <!-- springboot整合 redis --> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-redis</artifactid> <version>1.4.1.release</version> </dependency> </dependencies> <!-- 打包成一个可执行的jar包 --> <build> <plugins> <plugin> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-maven-plugin</artifactid> <version>1.4.1.release</version> </plugin> </plugins> </build> <!-- 项目发布仓库 --> <distributionmanagement> <repository> <id>releases</id> <name>nexus release repository</name> <url>http://localhost:8081/nexus/content/repositories/releases/</url> </repository> <snapshotrepository> <id>snapshots</id> <name>nexus snapshot repository</name> <url>http://localhost:8081/nexus/content/repositories/snapshots/</url> </snapshotrepository> </distributionmanagement> </project>
然后创建包,命名为com.spring.boot.base,在该包下创建databaseconfig.java文件,文件内容如下:
/** * copyright (c) windliven 2016 all rights reserved * * @author liyj * @date 2017年7月12日 上午9:53:09 * @since v1.0.0 */ package com.spring.boot.base; import javax.sql.datasource; import org.apache.ibatis.session.sqlsessionfactory; import org.mybatis.spring.sqlsessionfactorybean; import org.mybatis.spring.mapper.mapperscannerconfigurer; import org.slf4j.logger; import org.slf4j.loggerfactory; import org.springframework.context.annotation.bean; import org.springframework.context.annotation.configuration; import org.springframework.context.annotation.primary; import org.springframework.core.io.support.pathmatchingresourcepatternresolver; import org.springframework.jdbc.datasource.datasourcetransactionmanager; import org.springframework.transaction.platformtransactionmanager; import org.springframework.transaction.annotation.enabletransactionmanagement; import com.mchange.v2.c3p0.combopooleddatasource; /** * todo * * @author liyj * @date 2017年7月12日 上午9:53:09 * */ @configuration @enabletransactionmanagement public class databaseconfig { private final logger log = loggerfactory.getlogger(databaseconfig.class); @bean @primary public datasource getdatasource() throws exception { log.debug("config datasource"); combopooleddatasource cpds = new combopooleddatasource(); cpds.setdriverclass("com.mysql.jdbc.driver"); cpds.setjdbcurl("jdbc:mysql://localhost:3306/springboot"); cpds.setuser("root"); cpds.setpassword("root"); return cpds; } @bean public platformtransactionmanager gettransactionmanager() throws exception { return new datasourcetransactionmanager(getdatasource()); } @bean public sqlsessionfactory getsqlsessionfactory() throws exception { sqlsessionfactorybean sfb = new sqlsessionfactorybean(); sfb.setdatasource(getdatasource()); sfb.settypealiasespackage("com.spring.boot.entity"); pathmatchingresourcepatternresolver resolver = new pathmatchingresourcepatternresolver(); sfb.setmapperlocations(resolver .getresources("classpath:/mapper/*.xml")); return sfb.getobject(); } @bean public mapperscannerconfigurer getmapperscannerconfigurer() { mapperscannerconfigurer msc = new mapperscannerconfigurer(); msc.setbasepackage("com.mybloc.personal.mapper"); msc.setsqlsessionfactorybeanname("sqlsessionfactory"); return msc; } }
创建一个包,命名为com.spring.boot.dao,在该包下创建usermapper.java文件,该文件是一个接口类,内容如下:
/** * copyright (c) windliven 2016 all rights reserved * * @author liyj * @date 2017年7月11日 下午6:24:35 * @since v1.0.0 */ package com.spring.boot.dao; import java.util.list; import com.spring.boot.entity.userentity; /** * todo * * @author liyj * @date 2017年7月11日 下午6:24:35 * */ public interface usermapper { public void insertone(userentity entity); public void delete(userentity entity); public void update(userentity entity); public userentity getone(userentity entity); public list<userentity> getlist(); }
在src/main/resources目录下创建一个mapper文件目录,在该目录下创建usermapper.xml文件,内容如下:
<?xml version="1.0" encoding="utf-8" ?> <!doctype mapper public "-//mybatis.org//dtd mapper 3.0//en" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.spring.boot.dao.usermapper"> <!-- 增 --> <insert id="insertone" parametertype="userentity"> insert into user(id,name,pass,email,iphone) values(#{id},#{name},#{pass},#{email},#{iphone}) </insert> <!-- 删 --> <delete id="delete" parametertype="userentity"> delete from user where id = #{id} </delete> <!-- 改 --> <update id="update" parametertype="userentity"> update user set email = #{email},iphone = #{iphone} where id = #{id} </update> <!-- 查 --> <select id="getone" parametertype="userentity" resulttype="userentity"> select id, name, pass, email, iphone from user where id = #{id} </select> <!-- 查集合 --> <select id="getlist" resulttype="userentity"> select id, name, pass, email, iphone from user </select> </mapper>
接下来创建包,命名为:com.spring.boot.service,在该包下创建userserviceimpl.java文件,内容如下:
/** * copyright (c) windliven 2016 all rights reserved * * @author liyj * @date 2017年7月11日 下午4:18:55 * @since v1.0.0 */ package com.spring.boot.service; import java.util.list; import org.springframework.beans.factory.annotation.autowired; import org.springframework.stereotype.service; import org.springframework.transaction.annotation.transactional; import com.spring.boot.dao.usermapper; import com.spring.boot.entity.userentity; import com.spring.boot.inter.service.userservice; /** * todo * * @author liyj * @date 2017年7月11日 下午4:18:55 * */ @transactional(readonly=false) @service public class userserviceimpl implements userservice { @autowired private usermapper usermapper; /* * @see com.spring.boot.inter.service.userservice#insert(com.spring.boot.entity.userentity) */ public void insert(userentity entity) { usermapper.insertone(entity); system.out.println("insert"); } /* * @see com.spring.boot.inter.service.userservice#deleteentity(com.spring.boot.entity.userentity) */ public void deleteentity(userentity entity) { usermapper.delete(entity); system.out.println("deleteentity"); } /* * @see com.spring.boot.inter.service.userservice#deletebyid(java.lang.string) */ public void deletebyid(string id) { usermapper.delete(new userentity(id)); system.out.println("deletebyid"); } /* * @see com.spring.boot.inter.service.userservice#updateentity(com.spring.boot.entity.userentity) */ public void updateentity(userentity entity) { usermapper.update(entity); system.out.println("updateentity"); } /* * @see com.spring.boot.inter.service.userservice#updatebyid(java.lang.string) */ public void updatebyid(string id) { usermapper.update(new userentity(id)); system.out.println("updatebyid"); } /* * @see com.spring.boot.inter.service.userservice#getone(java.lang.string) */ @transactional(readonly=true) public userentity getone(string id) { return usermapper.getone(new userentity(id)); } /* * @see com.spring.boot.inter.service.userservice#getlist() */ @transactional(readonly=true) public list<userentity> getlist() { return usermapper.getlist(); } }
再创建一个包,命名为com.spring.boot.controller,在该包下创建usercontroller.java文件,内容如下:
/** * copyright (c) windliven 2016 all rights reserved * * @author liyj * @date 2017年7月11日 下午6:08:00 * @since v1.0.0 */ package com.spring.boot.controller; import java.util.collection; import org.springframework.beans.factory.annotation.autowired; import org.springframework.beans.factory.annotation.value; import org.springframework.cache.cache; import org.springframework.cache.cachemanager; import org.springframework.data.redis.core.redistemplate; import org.springframework.data.redis.core.valueoperations; import org.springframework.web.bind.annotation.requestmapping; import org.springframework.web.bind.annotation.requestmethod; import org.springframework.web.bind.annotation.requestparam; import org.springframework.web.bind.annotation.restcontroller; import com.spring.boot.entity.userentity; import com.spring.boot.inter.service.userservice; /** * todo * * @author liyj * @date 2017年7月11日 下午6:08:00 * */ //@springbootapplication(scanbasepackages = {"com.spring.boot"}) @restcontroller @requestmapping("/user") public class usercontroller { @autowired private userservice userservice; @autowired private cachemanager cachemanager; @autowired private redistemplate<string,string> redistemplate; @value("${testname}") private string name; @value("${testpass}") private string pass; @requestmapping(value = "/say", method = requestmethod.get) public string sayhello() { return "hello spring boot"; } @requestmapping(value = "/insert", method = requestmethod.get) public string insert() { userentity userentity = new userentity(); userentity.setid("111"); userentity.setname("liyj"); userentity.setpass("123456"); userentity.setemail("704603154@qq.com"); userentity.setiphone("18211140412"); userservice.insert(userentity); return "success"; } @requestmapping(value = "/one/get", method = requestmethod.get) public userentity getone(@requestparam string id) { return userservice.getone(id); } }
接着创建一个包,命名为com.spring.boot.start,在该包下创建startmain.java文件,内容如下:
/** * copyright (c) windliven 2016 all rights reserved * * @author liyj * @date 2017年7月11日 下午4:52:32 * @since v1.0.0 */ package com.spring.boot.start; import org.mybatis.spring.annotation.mapperscan; import org.springframework.boot.springapplication; import org.springframework.boot.autoconfigure.enableautoconfiguration; import org.springframework.context.annotation.componentscan; import org.springframework.context.annotation.configuration; /** * todo * * @author liyj * @date 2017年7月11日 下午4:52:32 * */ @enableautoconfiguration @componentscan(basepackages = "com.spring.boot") @configuration @mapperscan(value={"com.spring.boot.dao"}) public class startmain { public static void main(string[] args) { springapplication.run(startmain.class, args); } }
最后测试,启动startmain类中的main()方法,项目便启动了,可以正常的从浏览器中访问和测试。
总结
以上所述是小编给大家介绍的spring boot整合mybatis(一)实例代码,希望对大家有所帮助
上一篇: 新手用本十八招 即刻告别菜鸟
下一篇: 本本玩游戏时提示不支持OpenGL
推荐阅读
-
Spring Boot整合mybatis(一)实例代码
-
spring Boot与Mybatis整合优化详解
-
Spring Boot + Kotlin整合MyBatis的方法教程
-
Spring Boot集成Mybatis的实例代码(简洁版)
-
Spring Boot+Mybatis的整合过程
-
spring boot整合Shiro实现单点登录的示例代码
-
详解MongoDB和Spring整合的实例代码
-
Spring Boot与Kotlin 整合全文搜索引擎Elasticsearch的示例代码
-
spring boot springjpa 支持多个数据源的实例代码
-
Spring boot怎么整合Mybatis