|
| 1 | +# spring-boot-demo-orm-mybatis |
| 2 | + |
| 3 | +> 此 demo 演示了 Spring Boot 如何与原生的 mybatis 整合,使用了 mybatis 官方提供的脚手架 `mybatis-spring-boot-starter `可以很容易的和 Spring Boot 整合。 |
| 4 | +
|
| 5 | +## pom.xml |
| 6 | + |
| 7 | +```xml |
| 8 | +<?xml version="1.0" encoding="UTF-8"?> |
| 9 | +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 10 | + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> |
| 11 | + <modelVersion>4.0.0</modelVersion> |
| 12 | + |
| 13 | + <artifactId>spring-boot-demo-orm-mybatis</artifactId> |
| 14 | + <version>0.0.1-SNAPSHOT</version> |
| 15 | + <packaging>jar</packaging> |
| 16 | + |
| 17 | + <name>spring-boot-demo-orm-mybatis</name> |
| 18 | + <description>Demo project for Spring Boot</description> |
| 19 | + |
| 20 | + <parent> |
| 21 | + <groupId>com.xkcoding</groupId> |
| 22 | + <artifactId>spring-boot-demo</artifactId> |
| 23 | + <version>1.0.0-SNAPSHOT</version> |
| 24 | + </parent> |
| 25 | + |
| 26 | + <properties> |
| 27 | + <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> |
| 28 | + <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> |
| 29 | + <java.version>1.8</java.version> |
| 30 | + <mybatis.version>1.3.2</mybatis.version> |
| 31 | + </properties> |
| 32 | + |
| 33 | + <dependencies> |
| 34 | + <dependency> |
| 35 | + <groupId>org.mybatis.spring.boot</groupId> |
| 36 | + <artifactId>mybatis-spring-boot-starter</artifactId> |
| 37 | + <version>${mybatis.version}</version> |
| 38 | + </dependency> |
| 39 | + |
| 40 | + <dependency> |
| 41 | + <groupId>mysql</groupId> |
| 42 | + <artifactId>mysql-connector-java</artifactId> |
| 43 | + </dependency> |
| 44 | + |
| 45 | + <dependency> |
| 46 | + <groupId>org.projectlombok</groupId> |
| 47 | + <artifactId>lombok</artifactId> |
| 48 | + <optional>true</optional> |
| 49 | + </dependency> |
| 50 | + |
| 51 | + <dependency> |
| 52 | + <groupId>cn.hutool</groupId> |
| 53 | + <artifactId>hutool-all</artifactId> |
| 54 | + </dependency> |
| 55 | + |
| 56 | + <dependency> |
| 57 | + <groupId>com.google.guava</groupId> |
| 58 | + <artifactId>guava</artifactId> |
| 59 | + </dependency> |
| 60 | + |
| 61 | + <dependency> |
| 62 | + <groupId>org.springframework.boot</groupId> |
| 63 | + <artifactId>spring-boot-starter-test</artifactId> |
| 64 | + <scope>test</scope> |
| 65 | + </dependency> |
| 66 | + </dependencies> |
| 67 | + |
| 68 | + <build> |
| 69 | + <finalName>spring-boot-demo-orm-mybatis</finalName> |
| 70 | + <plugins> |
| 71 | + <plugin> |
| 72 | + <groupId>org.springframework.boot</groupId> |
| 73 | + <artifactId>spring-boot-maven-plugin</artifactId> |
| 74 | + </plugin> |
| 75 | + </plugins> |
| 76 | + </build> |
| 77 | + |
| 78 | +</project> |
| 79 | +``` |
| 80 | + |
| 81 | +## SpringBootDemoOrmMybatisApplication.java |
| 82 | + |
| 83 | +```java |
| 84 | +/** |
| 85 | + * <p> |
| 86 | + * 启动类 |
| 87 | + * </p> |
| 88 | + * |
| 89 | + * @package: com.xkcoding.orm.mybatis |
| 90 | + * @description: 启动类 |
| 91 | + * @author: yangkai.shen |
| 92 | + * @date: Created in 2018/11/8 10:52 |
| 93 | + * @copyright: Copyright (c) 2018 |
| 94 | + * @version: V1.0 |
| 95 | + * @modified: yangkai.shen |
| 96 | + */ |
| 97 | +@MapperScan(basePackages = {"com.xkcoding.orm.mybatis.mapper"}) |
| 98 | +@SpringBootApplication |
| 99 | +public class SpringBootDemoOrmMybatisApplication { |
| 100 | + |
| 101 | + public static void main(String[] args) { |
| 102 | + SpringApplication.run(SpringBootDemoOrmMybatisApplication.class, args); |
| 103 | + } |
| 104 | +} |
| 105 | +``` |
| 106 | + |
| 107 | +## UserMapper.java |
| 108 | + |
| 109 | +```java |
| 110 | +/** |
| 111 | + * <p> |
| 112 | + * User Mapper |
| 113 | + * </p> |
| 114 | + * |
| 115 | + * @package: com.xkcoding.orm.mybatis.mapper |
| 116 | + * @description: User Mapper |
| 117 | + * @author: yangkai.shen |
| 118 | + * @date: Created in 2018/11/8 10:54 |
| 119 | + * @copyright: Copyright (c) 2018 |
| 120 | + * @version: V1.0 |
| 121 | + * @modified: yangkai.shen |
| 122 | + */ |
| 123 | +@Mapper |
| 124 | +@Component |
| 125 | +public interface UserMapper { |
| 126 | + |
| 127 | + /** |
| 128 | + * 查询所有用户 |
| 129 | + * |
| 130 | + * @return 用户列表 |
| 131 | + */ |
| 132 | + @Select("SELECT * FROM orm_user") |
| 133 | + List<User> selectAllUser(); |
| 134 | + |
| 135 | + /** |
| 136 | + * 根据id查询用户 |
| 137 | + * |
| 138 | + * @param id 主键id |
| 139 | + * @return 当前id的用户,不存在则是 {@code null} |
| 140 | + */ |
| 141 | + @Select("SELECT * FROM orm_user WHERE id = #{id}") |
| 142 | + User selectUserById(@Param("id") Long id); |
| 143 | + |
| 144 | + /** |
| 145 | + * 保存用户 |
| 146 | + * |
| 147 | + * @param user 用户 |
| 148 | + * @return 成功 - {@code 1} 失败 - {@code 0} |
| 149 | + */ |
| 150 | + int saveUser(@Param("user") User user); |
| 151 | + |
| 152 | + /** |
| 153 | + * 删除用户 |
| 154 | + * |
| 155 | + * @param id 主键id |
| 156 | + * @return 成功 - {@code 1} 失败 - {@code 0} |
| 157 | + */ |
| 158 | + int deleteById(@Param("id") Long id); |
| 159 | + |
| 160 | +} |
| 161 | +``` |
| 162 | + |
| 163 | +## UserMapper.xml |
| 164 | + |
| 165 | +```xml |
| 166 | +<?xml version="1.0" encoding="UTF-8"?> |
| 167 | +<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> |
| 168 | +<mapper namespace="com.xkcoding.orm.mybatis.mapper.UserMapper"> |
| 169 | + |
| 170 | + <insert id="saveUser"> |
| 171 | + INSERT INTO `orm_user` (`name`, |
| 172 | + `password`, |
| 173 | + `salt`, |
| 174 | + `email`, |
| 175 | + `phone_number`, |
| 176 | + `status`, |
| 177 | + `create_time`, |
| 178 | + `last_login_time`, |
| 179 | + `last_update_time`) |
| 180 | + VALUES (#{user.name}, |
| 181 | + #{user.password}, |
| 182 | + #{user.salt}, |
| 183 | + #{user.email}, |
| 184 | + #{user.phoneNumber}, |
| 185 | + #{user.status}, |
| 186 | + #{user.createTime}, |
| 187 | + #{user.lastLoginTime}, |
| 188 | + #{user.lastUpdateTime}) |
| 189 | + </insert> |
| 190 | + |
| 191 | + <delete id="deleteById"> |
| 192 | + DELETE |
| 193 | + FROM `orm_user` |
| 194 | + WHERE `id` = #{id} |
| 195 | + </delete> |
| 196 | +</mapper> |
| 197 | +``` |
| 198 | + |
| 199 | +## UserMapperTest.java |
| 200 | + |
| 201 | +```java |
| 202 | +/** |
| 203 | + * <p> |
| 204 | + * UserMapper 测试类 |
| 205 | + * </p> |
| 206 | + * |
| 207 | + * @package: com.xkcoding.orm.mybatis.mapper |
| 208 | + * @description: UserMapper 测试类 |
| 209 | + * @author: yangkai.shen |
| 210 | + * @date: Created in 2018/11/8 11:25 |
| 211 | + * @copyright: Copyright (c) 2018 |
| 212 | + * @version: V1.0 |
| 213 | + * @modified: yangkai.shen |
| 214 | + */ |
| 215 | +@Slf4j |
| 216 | +public class UserMapperTest extends SpringBootDemoOrmMybatisApplicationTests { |
| 217 | + @Autowired |
| 218 | + private UserMapper userMapper; |
| 219 | + |
| 220 | + @Test |
| 221 | + public void selectAllUser() { |
| 222 | + List<User> userList = userMapper.selectAllUser(); |
| 223 | + Assert.assertTrue(CollUtil.isNotEmpty(userList)); |
| 224 | + log.debug("【userList】= {}", userList); |
| 225 | + } |
| 226 | + |
| 227 | + @Test |
| 228 | + public void selectUserById() { |
| 229 | + User user = userMapper.selectUserById(1L); |
| 230 | + Assert.assertNotNull(user); |
| 231 | + log.debug("【user】= {}", user); |
| 232 | + } |
| 233 | + |
| 234 | + @Test |
| 235 | + public void saveUser() { |
| 236 | + String salt = IdUtil.fastSimpleUUID(); |
| 237 | + User user = User.builder() .name( "testSave3") .password( SecureUtil.md5( "123456" + salt)) .salt(salt) .email( "[email protected]") .phoneNumber( "17300000003") .status( 1) .lastLoginTime( new DateTime()) .createTime( new DateTime()) .lastUpdateTime( new DateTime()) .build(); |
| 238 | + int i = userMapper.saveUser(user); |
| 239 | + Assert.assertEquals(1, i); |
| 240 | + } |
| 241 | + |
| 242 | + @Test |
| 243 | + public void deleteById() { |
| 244 | + int i = userMapper.deleteById(1L); |
| 245 | + Assert.assertEquals(1, i); |
| 246 | + } |
| 247 | +} |
| 248 | +``` |
| 249 | + |
| 250 | +## 参考 |
| 251 | + |
| 252 | +- Mybatis官方文档:http://www.mybatis.org/mybatis-3/zh/index.html |
| 253 | + |
| 254 | +- Mybatis官方脚手架文档:http://www.mybatis.org/spring-boot-starter/mybatis-spring-boot-autoconfigure/ |
| 255 | + |
| 256 | +- Mybatis整合Spring Boot官方demo:https://github.com/mybatis/spring-boot-starter/tree/master/mybatis-spring-boot-samples |
0 commit comments