boot-mybatis基于注解

spring boot mybatis 基于注解

基于spring boot 2.0.5.RELEASE

基础的CRUD注解

  • @Select
  • @Insert
  • @Update
  • @Delete

对象映射注解

  • @Results 返回多个字段映射.
  • @Result 单个字段映射.
  • @ResultMap 相当于<resultMap>标签

动态sql注解

  • @SelectProvider
  • @InsertProvider
  • @UpdateProvider
  • @DeleteProvider

案例

引入依赖
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 # 表与对象映射成驼峰式
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值