增加spring boot mybatis 整合用例
This commit is contained in:
parent
1caa454e62
commit
64296b35e6
@ -54,7 +54,7 @@ spring-cloud:Finchley.SR2
|
|||||||
| [spring-boot-tomcat](https://github.com/heibaiying/spring-samples-for-all/tree/master/spring-boot/spring-boot-tomcat) | spring-boot 整合外部容器(tomcat) | [Use Another Web Server](https://docs.spring.io/spring-boot/docs/2.1.1.RELEASE/reference/htmlsingle/#howto-use-another-web-server) |
|
| [spring-boot-tomcat](https://github.com/heibaiying/spring-samples-for-all/tree/master/spring-boot/spring-boot-tomcat) | spring-boot 整合外部容器(tomcat) | [Use Another Web Server](https://docs.spring.io/spring-boot/docs/2.1.1.RELEASE/reference/htmlsingle/#howto-use-another-web-server) |
|
||||||
| [spring-boot-jsp](https://github.com/heibaiying/spring-samples-for-all/tree/master/spring-boot/spring-boot-jsp) | spring-boot 整合 jsp | [JSP Limitations](https://docs.spring.io/spring-boot/docs/2.1.1.RELEASE/reference/htmlsingle/#boot-features-jsp-limitations) |
|
| [spring-boot-jsp](https://github.com/heibaiying/spring-samples-for-all/tree/master/spring-boot/spring-boot-jsp) | spring-boot 整合 jsp | [JSP Limitations](https://docs.spring.io/spring-boot/docs/2.1.1.RELEASE/reference/htmlsingle/#boot-features-jsp-limitations) |
|
||||||
| spring-boot-jpa | spring-boot jpa 的使用 | [Spring Data JPA](https://docs.spring.io/spring-data/jpa/docs/2.1.3.RELEASE/reference/html/) |
|
| spring-boot-jpa | spring-boot jpa 的使用 | [Spring Data JPA](https://docs.spring.io/spring-data/jpa/docs/2.1.3.RELEASE/reference/html/) |
|
||||||
| spring-boot-mybatis | spring-boot 整合 mybatis | [Mybatis-Spring](http://www.mybatis.org/spring/zh/index.html) |
|
| spring-boot-mybatis | spring-boot 整合 mybatis | [Mybatis-Spring](http://www.mybatis.org/spring/zh/index.html)<br/>[Mybatis-Spring-Boot-Autoconfigure](http://www.mybatis.org/spring-boot-starter/mybatis-spring-boot-autoconfigure/) |
|
||||||
| spring-boot-druid-mybtais | spring-boot 整合druid、mybatis | [Alibaba druid](https://github.com/alibaba/druid/wiki/%E5%B8%B8%E8%A7%81%E9%97%AE%E9%A2%98) |
|
| spring-boot-druid-mybtais | spring-boot 整合druid、mybatis | [Alibaba druid](https://github.com/alibaba/druid/wiki/%E5%B8%B8%E8%A7%81%E9%97%AE%E9%A2%98) |
|
||||||
| [spring-boot-redis](https://github.com/heibaiying/spring-samples-for-all/tree/master/spring-boot/spring-boot-redis) | spring-boot 整合 redis | [Working with NoSQL Technologies](https://docs.spring.io/spring-boot/docs/2.1.1.RELEASE/reference/htmlsingle/#boot-features-nosql) |
|
| [spring-boot-redis](https://github.com/heibaiying/spring-samples-for-all/tree/master/spring-boot/spring-boot-redis) | spring-boot 整合 redis | [Working with NoSQL Technologies](https://docs.spring.io/spring-boot/docs/2.1.1.RELEASE/reference/htmlsingle/#boot-features-nosql) |
|
||||||
| [spring-boot-mongodb](https://github.com/heibaiying/spring-samples-for-all/tree/master/spring-boot/spring-boot-mongodb) | spring-boot 整合 mongodb | [Working with NoSQL Technologies](https://docs.spring.io/spring-boot/docs/2.1.1.RELEASE/reference/htmlsingle/#boot-features-nosql) |
|
| [spring-boot-mongodb](https://github.com/heibaiying/spring-samples-for-all/tree/master/spring-boot/spring-boot-mongodb) | spring-boot 整合 mongodb | [Working with NoSQL Technologies](https://docs.spring.io/spring-boot/docs/2.1.1.RELEASE/reference/htmlsingle/#boot-features-nosql) |
|
||||||
|
56
spring-boot/spring-boot-mybatis/pom.xml
Normal file
56
spring-boot/spring-boot-mybatis/pom.xml
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<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>
|
||||||
|
<parent>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-parent</artifactId>
|
||||||
|
<version>2.1.1.RELEASE</version>
|
||||||
|
<relativePath/> <!-- lookup parent from repository -->
|
||||||
|
</parent>
|
||||||
|
<groupId>com.heibaiying</groupId>
|
||||||
|
<artifactId>spring-boot-mybatis</artifactId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
<name>spring-boot-mybatis</name>
|
||||||
|
<description>mybatis project for Spring Boot</description>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<java.version>1.8</java.version>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<!--spring 1.5 x 以上版本对应 mybatis 1.3.x (1.3.1)
|
||||||
|
关于更多spring-boot 与 mybatis 的版本对应可以参见 <a href="http://www.mybatis.org/spring-boot-starter/mybatis-spring-boot-autoconfigure/">-->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.mybatis.spring.boot</groupId>
|
||||||
|
<artifactId>mybatis-spring-boot-starter</artifactId>
|
||||||
|
<version>1.3.2</version>
|
||||||
|
</dependency>
|
||||||
|
<!--引入mysql驱动-->
|
||||||
|
<dependency>
|
||||||
|
<groupId>mysql</groupId>
|
||||||
|
<artifactId>mysql-connector-java</artifactId>
|
||||||
|
<version>8.0.13</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.projectlombok</groupId>
|
||||||
|
<artifactId>lombok</artifactId>
|
||||||
|
<optional>true</optional>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-test</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
</project>
|
@ -0,0 +1,14 @@
|
|||||||
|
package com.heibaiying.springboot;
|
||||||
|
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
|
||||||
|
@SpringBootApplication
|
||||||
|
public class SpringBootMybatisApplication {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.run(SpringBootMybatisApplication.class, args);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,38 @@
|
|||||||
|
package com.heibaiying.springboot.bean;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
import lombok.ToString;
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author : heibaiying
|
||||||
|
* @description : 实体类
|
||||||
|
*/
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@AllArgsConstructor
|
||||||
|
@NoArgsConstructor
|
||||||
|
@ToString
|
||||||
|
public class Programmer {
|
||||||
|
|
||||||
|
private int id;
|
||||||
|
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
private int age;
|
||||||
|
|
||||||
|
private float salary;
|
||||||
|
|
||||||
|
private Date birthday;
|
||||||
|
|
||||||
|
public Programmer(String name, int age, float salary, Date birthday) {
|
||||||
|
this.name = name;
|
||||||
|
this.age = age;
|
||||||
|
this.salary = salary;
|
||||||
|
this.birthday = birthday;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,25 @@
|
|||||||
|
package com.heibaiying.springboot.dao;
|
||||||
|
|
||||||
|
import com.heibaiying.springboot.bean.Programmer;
|
||||||
|
import org.apache.ibatis.annotations.*;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author : heibaiying
|
||||||
|
*/
|
||||||
|
@Mapper
|
||||||
|
public interface ProgrammerDao {
|
||||||
|
|
||||||
|
@Insert("insert into programmer (name, age, salary, birthday) VALUES (#{name}, #{age}, #{salary}, #{birthday})")
|
||||||
|
void save(Programmer programmer);
|
||||||
|
|
||||||
|
@Select("select * from programmer where name = #{id}")
|
||||||
|
Programmer selectById(int id);
|
||||||
|
|
||||||
|
@Update("update programmer set name=#{name},age=#{age},salary=#{salary},birthday=#{birthday} where id=#{id}")
|
||||||
|
int modify(Programmer programmer);
|
||||||
|
|
||||||
|
@Delete(" delete from programmer where id = #{id}")
|
||||||
|
void delete(int id);
|
||||||
|
}
|
@ -0,0 +1,22 @@
|
|||||||
|
package com.heibaiying.springboot.dao;
|
||||||
|
|
||||||
|
import com.heibaiying.springboot.bean.Programmer;
|
||||||
|
import org.apache.ibatis.annotations.Mapper;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author : heibaiying
|
||||||
|
*/
|
||||||
|
|
||||||
|
@Mapper
|
||||||
|
public interface ProgrammerMapper {
|
||||||
|
|
||||||
|
void save(Programmer programmer);
|
||||||
|
|
||||||
|
Programmer selectById(int id);
|
||||||
|
|
||||||
|
int modify(Programmer programmer);
|
||||||
|
|
||||||
|
void delete(int id);
|
||||||
|
}
|
@ -0,0 +1,55 @@
|
|||||||
|
spring:
|
||||||
|
datasource:
|
||||||
|
url: jdbc:mysql://127.0.0.1:3306/mysql?useUnicode=true&characterEncoding=utf-8
|
||||||
|
username: root
|
||||||
|
password: root
|
||||||
|
driver-class-name: com.mysql.cj.jdbc.Driver
|
||||||
|
|
||||||
|
# 如果不想配置数据源的话,以下关于连接池的配置就不是必须的
|
||||||
|
# spring-boot 2 默认采用高性能的 Hikari 作为连接池 更多配置可以参考 https://github.com/brettwooldridge/HikariCP#configuration-knobs-baby
|
||||||
|
type: com.zaxxer.hikari.HikariDataSource
|
||||||
|
hikari:
|
||||||
|
# 池中维护的最小空闲连接数
|
||||||
|
minimum-idle: 10
|
||||||
|
# 池中最大连接数,包括闲置和使用中的连接
|
||||||
|
maximum-pool-size: 20
|
||||||
|
# 此属性控制从池返回的连接的默认自动提交行为。默认为true
|
||||||
|
auto-commit: true
|
||||||
|
# 允许最长空闲时间
|
||||||
|
idle-timeout: 30000
|
||||||
|
# 此属性表示连接池的用户定义名称,主要显示在日志记录和JMX管理控制台中,以标识池和池配置。 默认值:自动生成
|
||||||
|
pool-name: custom-hikari
|
||||||
|
#此属性控制池中连接的最长生命周期,值0表示无限生命周期,默认1800000即30分钟
|
||||||
|
max-lifetime: 1800000
|
||||||
|
# 数据库连接超时时间,默认30秒,即30000
|
||||||
|
connection-timeout: 30000
|
||||||
|
# 连接测试sql 这个地方需要根据数据库方言差异而配置 例如 oracle 就应该写成 select 1 from dual
|
||||||
|
connection-test-query: SELECT 1
|
||||||
|
|
||||||
|
# mybatis 相关配置
|
||||||
|
mybatis:
|
||||||
|
# 指定 sql xml 文件的位置
|
||||||
|
mapper-locations: classpath*:mappers/*.xml
|
||||||
|
configuration:
|
||||||
|
# 当没有为参数提供特定的 JDBC 类型时,为空值指定 JDBC 类型。
|
||||||
|
# oracle数据库建议配置为JdbcType.NULL, 默认是Other
|
||||||
|
jdbc-type-for-null: 'null'
|
||||||
|
# 是否打印sql语句 调试的时候可以开启
|
||||||
|
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
|
||||||
|
|
||||||
|
|
||||||
|
# 注 1
|
||||||
|
# oracle
|
||||||
|
# driver="oracle.jdbc.driver.OracleDriver"
|
||||||
|
# url="jdbc:oracle:thin:@localhost:1521:数据库名"
|
||||||
|
# mysql
|
||||||
|
# driver="com.mysql.jdbc.Driver"
|
||||||
|
# url="jdbc:mysql://localhost/数据库名?[后接参数]"
|
||||||
|
|
||||||
|
|
||||||
|
# 注 2
|
||||||
|
# mybatis 配置更多说明可以参考settings http://www.mybatis.org/mybatis-3/zh/configuration.html
|
||||||
|
|
||||||
|
|
||||||
|
# 注 3
|
||||||
|
# spring boot 2.0 默认采用Hikari 作为连接池 github地址 https://github.com/brettwooldridge/HikariCP
|
@ -0,0 +1,22 @@
|
|||||||
|
<?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.heibaiying.springboot.dao.ProgrammerMapper">
|
||||||
|
|
||||||
|
<insert id="save">
|
||||||
|
insert into programmer (name, age, salary, birthday) VALUES (#{name}, #{age}, #{salary}, #{birthday})
|
||||||
|
</insert>
|
||||||
|
|
||||||
|
<select id="selectById" resultType="com.heibaiying.springboot.bean.Programmer">
|
||||||
|
select * from programmer where name = #{id}
|
||||||
|
</select>
|
||||||
|
|
||||||
|
<update id="modify">
|
||||||
|
update programmer set name=#{name},age=#{age},salary=#{salary},birthday=#{birthday} where id=#{id}
|
||||||
|
</update>
|
||||||
|
|
||||||
|
<delete id="delete">
|
||||||
|
delete from programmer where id = #{id}
|
||||||
|
</delete>
|
||||||
|
|
||||||
|
</mapper>
|
@ -0,0 +1,8 @@
|
|||||||
|
-- 建表语句
|
||||||
|
create table if not exists programmer (
|
||||||
|
id int primary key auto_increment,
|
||||||
|
name varchar(20),
|
||||||
|
age tinyint,
|
||||||
|
salary float,
|
||||||
|
birthday datetime
|
||||||
|
)
|
@ -0,0 +1,49 @@
|
|||||||
|
package com.heibaiying.springboot;
|
||||||
|
|
||||||
|
import com.heibaiying.springboot.bean.Programmer;
|
||||||
|
import com.heibaiying.springboot.dao.ProgrammerDao;
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
/***
|
||||||
|
* @description: 注解Sql测试类
|
||||||
|
*/
|
||||||
|
@RunWith(SpringRunner.class)
|
||||||
|
@SpringBootTest
|
||||||
|
public class MybatisAnnotationTest {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private ProgrammerDao programmerDao;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void save() {
|
||||||
|
programmerDao.save(new Programmer("xiaominng", 12, 3467.34f, new Date()));
|
||||||
|
programmerDao.save(new Programmer("xiaominng", 12, 3467.34f, new Date()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void modify() {
|
||||||
|
programmerDao.modify(new Programmer(1, "xiaolan", 21, 347.34f, new Date()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void selectByCondition() {
|
||||||
|
Programmer programmers = programmerDao.selectById(1);
|
||||||
|
System.out.println(programmers);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void delete() {
|
||||||
|
programmerDao.delete(3);
|
||||||
|
Programmer programmers = programmerDao.selectById(3);
|
||||||
|
Assert.assertNull(programmers);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,50 @@
|
|||||||
|
package com.heibaiying.springboot;
|
||||||
|
|
||||||
|
import com.heibaiying.springboot.bean.Programmer;
|
||||||
|
import com.heibaiying.springboot.dao.ProgrammerDao;
|
||||||
|
import com.heibaiying.springboot.dao.ProgrammerMapper;
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/***
|
||||||
|
* @description: xml Sql测试类
|
||||||
|
*/
|
||||||
|
@RunWith(SpringRunner.class)
|
||||||
|
@SpringBootTest
|
||||||
|
public class MybatisXmlTest {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private ProgrammerMapper mapper;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void save() {
|
||||||
|
mapper.save(new Programmer("xiaominng", 12, 3467.34f, new Date()));
|
||||||
|
mapper.save(new Programmer("xiaominng", 12, 3467.34f, new Date()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void modify() {
|
||||||
|
mapper.modify(new Programmer(1, "xiaohong", 112, 347.34f, new Date()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void selectByCondition() {
|
||||||
|
Programmer programmers = mapper.selectById(1);
|
||||||
|
System.out.println(programmers);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void delete() {
|
||||||
|
mapper.delete(2);
|
||||||
|
Programmer programmers = mapper.selectById(2);
|
||||||
|
Assert.assertNull(programmers);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user