lombook开启改功能
导入依赖
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>${mybatis.version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
配置application.xml
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql:///test?useSSL=false&serverTimezone=GMT%2B8
username: root
password: 2017
initialSize: 5
minIdle: 5
maxActive: 20
maxWait: 60000
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: SELECT user()
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
connection-properties: druid.stat.mergeSql:true;druid.stat.slowSqlMillis:5000
server:
port: 8080
mybatis:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
map-underscore-to-camel-case: true
mapper-locations: mapper/*Mapper.xml
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.test.springboottest.mapper.UserMapper">
<insert id="insertUser" parameterType="com.test.springboottest.entity.User">
insert into user (user_id, user_name, user_age, user_birth) values (
#{userId},#{userName},#{userAge},#{userBirth}
)
</insert>
</mapper>
user对象
package com.test.springboottest.entity;
import lombok.Data;
import lombok.ToString;
import java.io.Serializable;
import java.util.Date;
@Data
@ToString
public class User implements Serializable {
private static final long serialVersionUID = -509844197867602216L;
private Integer userId;
private String userName;
private Integer userAge;
private Date userBirth;
private Date createTime;
private Date updateTime;
}
UserMapper.java
package com.test.springboottest.mapper;
import com.test.springboottest.entity.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.util.List;
@Mapper
public interface UserMapper {
/**
* `user_id` int(11) NOT NULL,
* `user_name` varchar(30) DEFAULT NULL,
* `user_age` int(255) DEFAULT NULL,
* `user_birth` datetime DEFAULT NULL,
* `create_time` datetime DEFAULT CURRENT_TIMESTAMP,
* `update_time` datetime DEFAULT CURRENT_TIMESTAMP,
* PRIMARY KEY (`user_id`) USING BTREE
*/
@Select("select user_id,user_name,user_age,user_birth,create_time,update_time from user")
List<User> selectAll();
int insertUser(User user);
}
测试
package com.test.springboottest;
import com.alibaba.fastjson.JSON;
import com.test.springboottest.entity.User;
import com.test.springboottest.mapper.UserMapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import javax.annotation.Resource;
import javax.sql.DataSource;
import java.util.Date;
import java.util.List;
@SpringBootTest
class SpringboottestApplicationTests {
@Autowired
private DataSource dataSource;
@Resource
private UserMapper userMapper;
@Test
public void testSelectAll() throws Exception {
List<User> users = userMapper.selectAll();
System.out.println(JSON.toJSONString(users,true));
}
@Test
public void testInsertData() throws Exception {
User user = new User();
user.setUserId(45344);
user.setUserName("Gerry");
user.setUserAge(20);
user.setUserBirth(new Date());
int row = userMapper.insertUser(user);
System.out.println(row > 0 ? "成功" : "失败");
}
@Test
public void testDataSource() throws Exception {
/*Connection sourceConnection = dataSource.getConnection();
System.out.println(dataSource);
System.out.println(sourceConnection);*/
}
}