spring boot整合mybatis

逃离我推掉我的手 2022-06-12 08:59 116阅读 0赞

咳咳,之前的目录结构还在吧?不在了?尴尬那你自己的创建吧,继续spring boot整合mybatis!

按照一般SSM标准项目,项目结构如下,比较正规的项目,可能还会将实体和业务层接口独立出来作为一个服务jar包,此处省略,效果是一样的,有不懂的可以私聊我

Center

数据库怎么创建我就不教了,此处省略数据库(Mysql)安装,链接

SQL语句:

  1. CREATE TABLE `tb_favorite` (
  2. `id` bigint(16) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键',
  3. `product_id` bigint(16) unsigned DEFAULT NULL COMMENT '商品编号',
  4. `merchant_id` int(16) unsigned DEFAULT NULL COMMENT '商户编号',
  5. `status` int(1) DEFAULT NULL COMMENT '状态:1-收藏,2-取消收藏',
  6. `creator` varchar(32) DEFAULT NULL COMMENT '创建人',
  7. `create_time` datetime DEFAULT NULL COMMENT '创建时间',
  8. PRIMARY KEY (`id`),
  9. UNIQUE KEY `merchant_id` (`merchant_id`,`product_id`)
  10. ) ENGINE=InnoDB AUTO_INCREMENT=46714 DEFAULT CHARSET=utf8 COMMENT='个人收藏';

model:实体

  1. package com.xyy.model;
  2. import java.io.Serializable;
  3. import java.util.Date;
  4. public class Favorite implements Serializable {
  5. private static final long serialVersionUID = 1L;
  6. /** 主键 */
  7. private Long id;
  8. /** 商品编号 */
  9. private Long productId;
  10. /** 商户编号 */
  11. private Integer merchantId;
  12. /** 状态:1-收藏,2-取消收藏 */
  13. private Integer status;
  14. /** 创建人 */
  15. private String creator;
  16. /** 创建时间 */
  17. private Date createTime;
  18. /**
  19. *
  20. * @return 返回数据库表tb_favorite的id字段值
  21. */
  22. public Long getId() {
  23. return id;
  24. }
  25. /**
  26. * @param id 对应数据库表tb_favorite的id字段
  27. */
  28. public void setId(Long id) {
  29. this.id = id;
  30. }
  31. /**
  32. *
  33. * @return 返回数据库表tb_favorite的product_id字段值
  34. */
  35. public Long getProductId() {
  36. return productId;
  37. }
  38. /**
  39. * @param productId 对应数据库表tb_favorite的product_id字段
  40. */
  41. public void setProductId(Long productId) {
  42. this.productId = productId;
  43. }
  44. /**
  45. *
  46. * @return 返回数据库表tb_favorite的merchant_id字段值
  47. */
  48. public Integer getMerchantId() {
  49. return merchantId;
  50. }
  51. /**
  52. * @param merchantId 对应数据库表tb_favorite的merchant_id字段
  53. */
  54. public void setMerchantId(Integer merchantId) {
  55. this.merchantId = merchantId;
  56. }
  57. /**
  58. *
  59. * @return 返回数据库表tb_favorite的status字段值
  60. */
  61. public Integer getStatus() {
  62. return status;
  63. }
  64. /**
  65. * @param status 对应数据库表tb_favorite的status字段
  66. */
  67. public void setStatus(Integer status) {
  68. this.status = status;
  69. }
  70. /**
  71. *
  72. * @return 返回数据库表tb_favorite的creator字段值
  73. */
  74. public String getCreator() {
  75. return creator;
  76. }
  77. /**
  78. * @param creator 对应数据库表tb_favorite的creator字段
  79. */
  80. public void setCreator(String creator) {
  81. this.creator = creator;
  82. }
  83. /**
  84. *
  85. * @return 返回数据库表tb_favorite的create_time字段值
  86. */
  87. public Date getCreateTime() {
  88. return createTime;
  89. }
  90. /**
  91. * @param createTime 对应数据库表tb_favorite的create_time字段
  92. */
  93. public void setCreateTime(Date createTime) {
  94. this.createTime = createTime;
  95. }
  96. }

dao:持久化层接口

  1. package com.xyy.dao;
  2. import org.apache.ibatis.annotations.Mapper;
  3. import com.xyy.model.Favorite;
  4. /**
  5. * Favorite持久化层接口
  6. * @ClassName: FavoriteMapper
  7. * @author wujing
  8. * @date 2017-07-13 15:09:50
  9. */
  10. @Mapper
  11. public interface FavoriteMapper{
  12. Favorite selectByPrimaryKey(Long id);
  13. }

mapper:持久化层映射文件

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
  3. <mapper namespace="com.xyy.dao.FavoriteMapper" >
  4. <resultMap id="BaseResultMap" type="com.xyy.model.Favorite" >
  5. <id column="id" property="id" jdbcType="BIGINT" />
  6. <result column="product_id" property="productId" jdbcType="BIGINT" />
  7. <result column="merchant_id" property="merchantId" jdbcType="INTEGER" />
  8. <result column="status" property="status" jdbcType="INTEGER" />
  9. <result column="creator" property="creator" jdbcType="VARCHAR" />
  10. <result column="create_time" property="createTime" jdbcType="TIMESTAMP" />
  11. </resultMap>
  12. <sql id="Base_Column_List" >
  13. id, product_id, merchant_id, status, creator, create_time
  14. </sql>
  15. <sql id="Alias_Column_List" >
  16. f.id, f.product_id, f.merchant_id, f.status, f.creator, f.create_time
  17. </sql>
  18. <insert id="insertSelective" parameterType="com.xyy.model.Favorite" useGeneratedKeys="true" keyProperty="id" keyColumn="id" >
  19. insert into tb_favorite
  20. <trim prefix="(" suffix=")" suffixOverrides="," >
  21. <if test="productId != null" >
  22. product_id,
  23. </if>
  24. <if test="merchantId != null" >
  25. merchant_id,
  26. </if>
  27. <if test="status != null" >
  28. status,
  29. </if>
  30. <if test="creator != null" >
  31. creator,
  32. </if>
  33. <if test="createTime != null" >
  34. create_time,
  35. </if>
  36. </trim>
  37. <trim prefix="values (" suffix=")" suffixOverrides="," >
  38. <if test="productId != null" >
  39. #{productId,jdbcType=BIGINT},
  40. </if>
  41. <if test="merchantId != null" >
  42. #{merchantId,jdbcType=INTEGER},
  43. </if>
  44. <if test="status != null" >
  45. #{status,jdbcType=INTEGER},
  46. </if>
  47. <if test="creator != null" >
  48. #{creator,jdbcType=VARCHAR},
  49. </if>
  50. <if test="createTime != null" >
  51. #{createTime,jdbcType=TIMESTAMP},
  52. </if>
  53. </trim>
  54. </insert>
  55. <delete id="deleteByPrimaryKey" parameterType="java.lang.Long" >
  56. delete from tb_favorite
  57. where id = #{id,jdbcType=BIGINT}
  58. </delete>
  59. <delete id="batchDeleteByIds" parameterType="java.util.List" >
  60. delete from tb_favorite
  61. where id in
  62. <foreach item="id" collection="list" open="(" separator="," close=")" >
  63. #{id}
  64. </foreach>
  65. </delete>
  66. <update id="updateByPrimaryKeySelective" parameterType="com.xyy.model.Favorite" >
  67. update tb_favorite
  68. <set >
  69. <if test="productId != null" >
  70. product_id = #{productId,jdbcType=BIGINT},
  71. </if>
  72. <if test="merchantId != null" >
  73. merchant_id = #{merchantId,jdbcType=INTEGER},
  74. </if>
  75. <if test="status != null" >
  76. status = #{status,jdbcType=INTEGER},
  77. </if>
  78. <if test="creator != null" >
  79. creator = #{creator,jdbcType=VARCHAR},
  80. </if>
  81. <if test="createTime != null" >
  82. create_time = #{createTime,jdbcType=TIMESTAMP},
  83. </if>
  84. </set>
  85. where id = #{id,jdbcType=BIGINT}
  86. </update>
  87. <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Long" >
  88. select
  89. <include refid="Base_Column_List" />
  90. from tb_favorite
  91. where id = #{id,jdbcType=BIGINT}
  92. </select>
  93. <select id="selectList" resultMap="BaseResultMap" parameterType="java.util.Map" >
  94. select
  95. <include refid="Base_Column_List" />
  96. from tb_favorite
  97. <where >
  98. <if test="id != null" >
  99. and id = #{id,jdbcType=BIGINT}
  100. </if>
  101. <if test="productId != null" >
  102. and product_id = #{productId,jdbcType=BIGINT}
  103. </if>
  104. <if test="merchantId != null" >
  105. and merchant_id = #{merchantId,jdbcType=INTEGER}
  106. </if>
  107. <if test="status != null" >
  108. and status = #{status,jdbcType=INTEGER}
  109. </if>
  110. <if test="creator != null and creator != ''" >
  111. and creator = #{creator,jdbcType=VARCHAR}
  112. </if>
  113. <if test="createTime != null" >
  114. and create_time = #{createTime,jdbcType=TIMESTAMP}
  115. </if>
  116. </where>
  117. <if test="property != null and property !=''" >
  118. order by ${property} ${direction}
  119. </if>
  120. </select>
  121. <select id="selectCount" resultType="java.lang.Integer" parameterType="java.util.Map" >
  122. select count(1)
  123. from tb_favorite
  124. <where >
  125. <if test="id != null" >
  126. and id = #{id,jdbcType=BIGINT}
  127. </if>
  128. <if test="productId != null" >
  129. and product_id = #{productId,jdbcType=BIGINT}
  130. </if>
  131. <if test="merchantId != null" >
  132. and merchant_id = #{merchantId,jdbcType=INTEGER}
  133. </if>
  134. <if test="status != null" >
  135. and status = #{status,jdbcType=INTEGER}
  136. </if>
  137. <if test="creator != null and creator != ''" >
  138. and creator = #{creator,jdbcType=VARCHAR}
  139. </if>
  140. <if test="createTime != null" >
  141. and create_time = #{createTime,jdbcType=TIMESTAMP}
  142. </if>
  143. </where>
  144. </select>
  145. </mapper>

service:业务层接口

  1. package com.xyy.service;
  2. import com.xyy.model.Favorite;
  3. /**
  4. * Favorite业务层接口
  5. * @ClassName: FavoriteService
  6. * @author wujing
  7. * @date 2017-07-13 15:09:50
  8. */
  9. public interface FavoriteService{
  10. Favorite selectByPrimaryKey(Long id);
  11. }

impl:业务层接口实现类

  1. package com.xyy.service.impl;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.stereotype.Service;
  4. import org.springframework.transaction.annotation.Transactional;
  5. import com.xyy.dao.FavoriteMapper;
  6. import com.xyy.model.Favorite;
  7. import com.xyy.service.FavoriteService;
  8. /**
  9. * Favorite业务层实现类
  10. * @ClassName: FavoriteServiceImpl
  11. * @author wujing
  12. * @date 2017-07-13 15:09:50
  13. */
  14. @Transactional
  15. @Service("favoriteService")
  16. public class FavoriteServiceImpl implements FavoriteService {
  17. @Autowired
  18. private FavoriteMapper favoriteMapper;
  19. public Favorite selectByPrimaryKey(Long id) {
  20. return favoriteMapper.selectByPrimaryKey(id);
  21. }
  22. }

controller:控制层

  1. package com.xyy.controller;
  2. import java.util.HashMap;
  3. import java.util.Map;
  4. import org.slf4j.Logger;
  5. import org.slf4j.LoggerFactory;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.ui.ModelMap;
  8. import org.springframework.web.bind.annotation.PathVariable;
  9. import org.springframework.web.bind.annotation.RequestMapping;
  10. import org.springframework.web.bind.annotation.RestController;
  11. import org.springframework.web.servlet.ModelAndView;
  12. import com.xyy.model.Favorite;
  13. import com.xyy.service.FavoriteService;
  14. /**
  15. * Favorite控制层
  16. * @ClassName: FavoriteController
  17. * @author wujing
  18. * @date 2017-07-13 15:09:50
  19. */
  20. @RestController
  21. @RequestMapping("/favorite")
  22. public class FavoriteController{
  23. private static final Logger LOGGER = LoggerFactory.getLogger(FavoriteController.class);
  24. @Autowired
  25. private FavoriteService favoriteService;
  26. /**
  27. * Favorite编辑
  28. * @Title: update
  29. * @param favorite 修改对象
  30. * @return Object
  31. * @author wujing
  32. * @date 2017-07-13 15:09:50
  33. */
  34. @RequestMapping("/detail/{id}")
  35. public Object detail(@PathVariable Long id,ModelMap modelMap) {
  36. Map<String,Object> resultMap = new HashMap<String,Object>();
  37. try {
  38. Favorite tempFavorite = favoriteService.selectByPrimaryKey(id);
  39. resultMap.put("status", "success");
  40. resultMap.put("data", tempFavorite);
  41. } catch (Exception e) {
  42. resultMap.put("status", "error");
  43. resultMap.put("errorMsg", "系统异常");
  44. LOGGER.error("Favorite添加异常", e);
  45. }
  46. return resultMap;
  47. }
  48. }

每个代码的具体实现和普通的spring项目代码没有区别,唯一的区别

1:pom.xml文件,需要加入依赖,具体如下:

  1. <dependency>
  2. <groupId>org.mybatis.spring.boot</groupId>
  3. <artifactId>mybatis-spring-boot-starter</artifactId>
  4. <version>1.1.1</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>mysql</groupId>
  8. <artifactId>mysql-connector-java</artifactId>
  9. </dependency>

mybatis-spring-boot-starter:mybatis与spring boot的依赖包

mysql-connector-java:数据库驱动

2:src/main/resources/application.properties需要加入数据库以及mybatis相关的配置,我比较中意key-value的方式,符合我们之前的习惯,yml的方式,就此忽略掉了,有需要了解的另行百度

注:一下配置根据自行情况更改,如跟着我做的,只需要更改datasource相关即可

  1. #数据库配置文件
  2. spring.datasource.url=jdbc:mysql://localhost:3306/xyy_v2?useUnicode=true&characterEncoding=utf8&allowMultiQueries=true
  3. spring.datasource.username=admin
  4. spring.datasource.password=admin
  5. spring.datasource.driver-class-name=com.mysql.jdbc.Driver
  6. #mybatis配置文件
  7. mybatis.mapperLocations=classpath:com/xyy/mapper/*Mapper.xml
  8. mybatis.type-aliases-package=com.xyy.model

同样右键运行main主入口,浏览器访问
Center 1

自此第三节结束,第四节:spring boot整合freemark

发表评论

表情:
评论列表 (有 0 条评论,116人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Spring boot整合Mybatis

    时隔两个月的再来写博客的感觉怎么样呢,只能用“棒”来形容了。闲话少说,直接入正题,之前的博客中有说过,将spring与mybatis整个后开发会更爽,基于现在springboo

    相关 Spring boot整合Mybatis

    ​ 抽空写个小demo,顺便温习下知识,自从用了Spring boot之后,怎一个爽字了得,开发起来太舒服了,目前Springboot已经成为了开发界的主流框架了,在此就将Sp