公众号merlinsea
Mybatis使用注解开发
使用建议:注解的开发比较简单,如果只是简单的pojo数据库操作,建议使用注解的方式 ;如果是关联查询,比如嵌套查询,建议使用编写xml文件的方式 ;在实际的生产开发过程中,通常注解方式和xml文件方式会结合使用!!!
1、引入mybatis和数据库相关依赖
<!--加入mybatis依赖--> <!-- 引入starter--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.2</version> </dependency> <!-- MySQL的JDBC驱动包 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <!-- 引入第三方数据源 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.6</version> </dependency>
2、在application.properties中编写好mybatis和数据库的配置
#=================================数据库相关配置==================================== #可以自动识别需要使用的数据库驱动类,如果需要指定其他数据库驱动则可以开启这个配置 #spring.datasource.driver-class-name =com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/user?useUnicode=true&characterEncoding=utf-8 spring.datasource.username =root spring.datasource.password =password #如果不使用默认的数据源 (com.zaxxer.hikari.HikariDataSource) spring.datasource.type =com.alibaba.druid.pool.DruidDataSource # mybatis 下划线转驼峰配置,两者都可以 #mybatis.configuration.mapUnderscoreToCamelCase=true mybatis.configuration.map-underscore-to-camel-case=true #打印sql,方便调试 mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
3、启动类中开启包扫描@MapperScan()
注:@MapperScan("org.lianglin.mapper")表示spring的ioc容器会自动扫描org.lianglin.mapper包下的所有类并示例化。
@SpringBootApplication @MapperScan("org.lianglin.mapper") public class XdvideoApplication { public static void main(String[] args) { SpringApplication.run(XdvideoApplication.class, args); } }
4、通过注解的方式配置mybatis的sql查询语句【注解使用核心】
/** * video数据访问层 */ public interface VideoMapper { //查询 @Select("select * from video") List<Video> findAll(); @Select("SELECT * FROM video WHERE id = #{id}") Video findById(int id); //修改 //@Update("UPDATE video SET title=#{title} WHERE id =#{id}") @UpdateProvider(type = VideoProvider.class,method = "updateVideo") int update(Video Video); //删除 @Delete("DELETE FROM video WHERE id =#{id}") int delete(int id); /** * @Insert 中的#{}会自动用get取video中对应的值 * @Option 会自动返回数据库中的自增id * @param video * @return */ @Insert("INSERT INTO `video` ( `title`, `summary`, " + "`cover_img`, `view_num`, `price`, `create_time`," + " `online`, `point`)" + "VALUES" + "(#{title}, #{summary}, #{coverImg}, #{viewNum}, #{price},#{createTime}" + ",#{online},#{point});") @Options(useGeneratedKeys=true, keyProperty="id", keyColumn="id") int save(Video video); }
/** * 订单dao层 */ public interface VideoOrderMapper { /** * 保存订单,返回包含主键 * @param videoOrder * @return */ @Insert("INSERT INTO `video_order` (`openid`, `out_trade_no`, `state`, `create_time`," + " `notify_time`, `total_fee`, `nickname`, `head_img`, `video_id`, `video_title`," + " `video_img`, `user_id`, `ip`, `del`)" + "VALUES" + "(#{openid},#{outTradeNo},#{state},#{createTime},#{notifyTime},#{totalFee}," + "#{nickname},#{headImg},#{videoId},#{videoTitle},#{videoImg},#{userId},#{ip},#{del});") @Options(useGeneratedKeys = true,keyProperty = "id",keyColumn = "id") int insert(VideoOrder videoOrder); /** * 根据主键查找订单 * @param id * @return */ @Select("select * from video_order where id=#{order_id} and del=0") VideoOrder findById(@Param("order_id") int id); /** * 根据交易订单号获取订单对象 * @param outTradeNo * @return */ @Select("select * from video_order where out_trade_no=#{out_trade_no} and del=0") VideoOrder findByOutTradeNo(@Param("out_trade_no") String outTradeNo); /** * 逻辑删除订单 * @param id * @param userId * @return */ @Update("update video_order set del=0 where id=#{id} and user_id =#{userId}") int del(@Param("id") int id, @Param("userId") int userId); /** * 查找我的全部订单 * @param userId * @return */ @Select("select * from video_order where user_id =#{userId}") List<VideoOrder> findMyOrderList(int userId); /** * 根据订单流水号更新 * @param videoOrder * @return */ @Update("update video_order set state=#{state}, notify_time=#{notifyTime}, openid=#{openid}" + " where out_trade_no=#{outTradeNo} and state=0 and del=0") int updateVideoOderByOutTradeNo(VideoOrder videoOrder); }
数据库层的对应实体类
public class Video implements Serializable { private Integer id; private String title; private String summary; private String coverImg; private Integer viewNum; private Integer price; private java.util.Date createTime; private Integer online; private Double point; /** setter/getter方法自动生成 */ }