sprig-boot是一個微服務架構,加快了spring工程快速開發,以及簡便了配置。接下來開始spring-boot與mybatis的整合。
1、創建一個maven工程命名為spring-boot-entity,pom.xml文件配置如下:
1
|
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
<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文件配置如下:
1
|
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
<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文件內容如下:
1
|
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
|
<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文件,文件內容如下:
1
|
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
/** * 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文件,該文件是一個接口類,內容如下:
1
|
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
/** * 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文件,內容如下:
1
|
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
<?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文件,內容如下:
1
|
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
|
/** * 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文件,內容如下:
1
|
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
/** * 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.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文件,內容如下:
1
|
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
/** * 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(一)實例代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網站的支持!
原文鏈接:http://www.cnblogs.com/liysoftware/archive/2017/07/20/7210007.html