增加spring boot mybatis 整合用例

This commit is contained in:
罗祥
2019-01-02 18:53:59 +08:00
parent 1caa454e62
commit 64296b35e6
11 changed files with 340 additions and 1 deletions

View File

@@ -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);
}
}

View File

@@ -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;
}
}

View File

@@ -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);
}

View File

@@ -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);
}

View File

@@ -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

View File

@@ -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>

View File

@@ -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
)

View File

@@ -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);
}
}

View File

@@ -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);
}
}