mybatis

299 阅读2分钟

mybatis-generator

mybatis annotation注解方式写sql

  1. 这个例子很好 juejin.cn/post/684490… 2.贴一个mapper层(dao层的代码)
@Mapper
public interface MoneyMapper {

    // 支持主键写回到po

    //这个@Options的目的是把插入的主键回写到po实体类
    @Options(useGeneratedKeys = true, keyProperty = "po.id", keyColumn = "id")
    @Insert("insert into money (name, money, is_deleted) values (#{po.name}, #{po.money}, #{po.isDeleted})")
    int savePo(@Param("po") MoneyPo po);

    @Select("select * from money where name=#{name}")
    @Results({@Result(property = "id", column = "id", id = true, jdbcType = JdbcType.INTEGER),
            @Result(property = "name", column = "name", jdbcType = JdbcType.VARCHAR),
            @Result(property = "money", column = "money", jdbcType = JdbcType.INTEGER),
            @Result(property = "isDeleted", column = "is_deleted", jdbcType = JdbcType.TINYINT),
            @Result(property = "createAt", column = "create_at", jdbcType = JdbcType.TIMESTAMP),
            @Result(property = "updateAt", column = "update_at", jdbcType = JdbcType.TIMESTAMP)})
    List<MoneyPo> findByName(@Param("name") String name);

    @Update("update money set money=money+#{money} where id=#{id}")
    int addMoney(@Param("id") int id, @Param("money") int money);

    @Delete("delete from money where id = #{id,jdbcType=INTEGER}")
    int delPo(@Param("id") int id);

    @Select("<script> select * from money " +
            "<trim prefix=\"WHERE\" prefixOverrides=\"AND | OR\">" +
            "   <if test=\"id != null\">" +
            "       id = #{id}" +
            "   </if>" +
            "   <if test=\"name != null\">" +
            "       AND name=#{name}" +
            "   </if>" +
            "   <if test=\"money != null\">" +
            "       AND money=#{money}" +
            "   </if>" +
            "</trim>" +
            "</script>")
    @Results({@Result(property = "id", column = "id", id = true, jdbcType = JdbcType.INTEGER),
            @Result(property = "name", column = "name", jdbcType = JdbcType.VARCHAR),
            @Result(property = "money", column = "money", jdbcType = JdbcType.INTEGER),
            @Result(property = "isDeleted", column = "is_deleted", jdbcType = JdbcType.TINYINT),
            @Result(property = "createAt", column = "create_at", jdbcType = JdbcType.TIMESTAMP),
            @Result(property = "updateAt", column = "update_at", jdbcType = JdbcType.TIMESTAMP)})
    List<MoneyPo> findByPo(MoneyPo po);
}

3.还有一个地方很好,这个项目是虽然是springboot项目,但是没有添加web依赖,所以不能用浏览器url的方式调试,那就可以使用它的这个方式

@SpringBootApplication
public class Application {

    //下边这个MoneyRepository虽然是使用的@Repository,类似于@Service
    public Application(MoneyRepository repository) {
        repository.testMapper();
    }

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}

如果是纯maven项目,那如何调试呢,----->在Application.java 上写main

mybatis多数据源:动态切换数据源+动态加载数据源

www.cnblogs.com/masonlee/p/…

有代码

第二篇是动态切换

多数据源换成druid最关键的一点是
//return DataSourceBuilder.create().build(); return new DruidDataSource();

第三篇是动态加载