基于spring boot 2.0.5.RELEASE
标签dependencies {
compile('org.springframework.boot:spring-boot-starter-web')
compile('org.mybatis.spring.boot:mybatis-spring-boot-starter:1.3.2')
compile group: 'org.projectlombok', name: 'lombok', version: '1.18.2'
compile group: 'mysql', name: 'mysql-connector-java', version: '6.0.6'
compile group: 'com.alibaba', name: 'druid', version: '1.1.11'
compile group: 'com.alibaba', name: 'fastjson', version: '1.2.49'
testCompile('org.springframework.boot:spring-boot-starter-test')
}
实体`UserDo.java:
@Data
public class UserDo extends BaseDo{
private Integer id;
private String username;
private String password;
private String mobile;
private String name;
}
Dao层UserDao.java
:
public interface UserDao {
@Select("select * from t_user where id=#{id}")
UserDo getUserById(@Param("id") int id);
@Results(id = "getUserByUserNameMap",value = {
@Result(column = "username",property = "userName"),
@Result(column = "password",property = "password"),
@Result(column = "is_delete",property = "isDelete"),
@Result(column = "gmt_create",property = "gmtCreate"),
})
@Select("select username,password,is_delete,gmt_create from t_user where username=#{userName}")
Map<String, Object>getUserByUserName(String userName);
@ResultMap(value = "getUserByUserNameMap")
@Select("select username,password,is_delete,gmt_create from t_user where mobile=#{mobile}")
Map<String, Object>getUserByMobile(String mobile);
@Insert("INSERT INTO t_user (`username`, `password`, `mobile`, `name`, `is_delete`, `gmt_modified`, `gmt_create`) VALUES ( #{username}, #{password}, #{mobile}, #{name}, '0', now(), now());")
@Options(keyProperty = "id", useGeneratedKeys = true)
public int insert(UserDo userDo);
@SelectProvider(type = UserSqlProvider.class, method = "selectUserByName")
UserDo getUserByName(String name);
@UpdateProvider(type = UserSqlProvider.class, method = "updateUser")
int updateUser(UserDo userDo);
}
动态sql生成提供类:
public class UserSqlProvider {
public String selectUserByName(){
return "select * from t_user where name=#{name}";
}
public String updateUser(UserDo userDo){
return new SQL(){{
UPDATE("t_user");
if (userDo.getName()!=null){
SET("name=#{name}");
}
if (userDo.getUsername()!=null){
SET("username=#{username}");
}
if (userDo.getMobile() != null){
SET("mobile=#{mobile}");
}
if (userDo.getPassword() != null){
SET("password=#{password}");
}
WHERE("id=#{id}");
}}.toString();
}
}
扫描Dao层:
@SpringBootApplication
@MapperScan(basePackages = {"com.example.demo.persistent.dao"})
@EnableTransactionManagement
public class SpringBootMybatisApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootMybatisApplication.class, args);
}
}
application.yml
配置:
spring:
datasource:
url: jdbc:mysql://localhost:3306/db_spring?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: *****
logging:
level:
com.example.demo.persistent.dao: debug
mybatis:
configuration:
map-underscore-to-camel-case: true # 表与对象映射成驼峰式