SpringBoot(7)、整合Mybatis
2017-04-10 本文已影响251人
编程界的小学生
先看一下 整体目录结构
![](https://img.haomeiwen.com/i4582242/ab03b30b7cf9b838.png)
贴代码
Application
package com.ctw.springboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @author 15620646321@163.com
* @date 2017年4月10日
*/
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
POM
<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.springboot</groupId>
<artifactId>springboot-mybatis</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springboot-mybatis</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<boot.version>1.5.2.RELEASE</boot.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>${boot.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.2.0</version>
</dependency>
<!-- mysql 驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>
<!-- 数据库连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.18</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>${boot.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.properties
# dataSource
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/zd
spring.datasource.password=
spring.datasource.username=root
#初始化大小,最小,最大
spring.datasource.initialSize=10
spring.datasource.minIdle=10
spring.datasource.maxActive=100
# mybatis
mybatis.mapper-locations=classpath*:mapper/*Mapper.xml
mybatis.typeAliasesPackage=com.ctw.springboot.entity
#mapper
mapper.not-empty=false
mapper.identity=MYSQL
User
package com.ctw.springboot.entity;
/**
* @author 15620646321@163.com
* @date 2017年4月5日
*/
public class User {
private Integer id;
private String username;
private Integer age;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
UserMapper
package com.ctw.springboot.mapper;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import com.ctw.springboot.entity.User;
/**
* @author 15620646321@163.com
* @date 2017年4月5日
*/
@Mapper
public interface UserMapper {
int save(User user);
User selectById(Integer id);
int updateById(User user);
int deleteById(Integer id);
List<User> findAll();
}
@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.ctw.springboot.mapper.UserMapper" >
<insert id="save" parameterType="com.ctw.springboot.entity.User">
INSERT INTO user(username, age) VALUES(#{username,jdbcType=VARCHAR},#{age,jdbcType=NUMERIC})
</insert>
<select id="selectById" resultType="com.ctw.springboot.entity.User">
SELECT * FROM user WHERE id = #{id, jdbcType=NUMERIC}
</select>
<update id="updateById" parameterType="com.ctw.springboot.entity.User">
UPDATE user SET
username = #{username,jdbcType=VARCHAR} ,
age = #{age,jdbcType=NUMERIC}
WHERE
id = #{id,jdbcType=NUMERIC}
</update>
<delete id="deleteById">
DELETE FROM user WHERE id = #{id,jdbcType=NUMERIC}
</delete>
<select id="findAll" resultType="com.ctw.springboot.entity.User">
SELECT * FROM user
</select>
</mapper>
TestDemo
package com.ctw.springboot.test;
import java.util.List;
import javax.annotation.Resource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.ctw.springboot.entity.User;
import com.ctw.springboot.mapper.UserMapper;
/**
* Test CRUD
*
* @author 15620646321@163.com
* @date 2017年4月5日
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public class TestDemo {
@Resource
private UserMapper userMapper;
@Test
public void testSave() {
User user = new User();
user.setUsername("chentw");
user.setAge(11);
int result = userMapper.save(user);
System.out.println(result);
}
@Test
public void testSelectById() {
User user = userMapper.selectById(7);
System.out.println(user.getUsername());
}
@Test
public void testFindAll() {
List<User> user = userMapper.findAll();
user.stream().forEach(x -> System.out.println(x.getId()));
}
@Test
public void testUpdateById() {
User user = new User();
user.setId(6);
user.setAge(12);
user.setUsername("chen");
int result = userMapper.updateById(user);
System.out.println(result);
}
@Test
public void testDeleteById() {
int result = userMapper.deleteById(4);
System.out.println(result);
}
}
若有兴趣,欢迎来加入群,【Java初学者学习交流群】:458430385,此群有Java开发人员、UI设计人员和前端工程师。有问必答,共同探讨学习,一起进步!
欢迎关注我的微信公众号【Java码农社区】,会定时推送各种干货:
![](http://upload-images.jianshu.io/upload_images/4582242-ca4a357ae859b1aa.jpg)