spring boot整合mybatis
咳咳,之前的目录结构还在吧?不在了?那你自己的创建吧,继续spring boot整合mybatis!
按照一般SSM标准项目,项目结构如下,比较正规的项目,可能还会将实体和业务层接口独立出来作为一个服务jar包,此处省略,效果是一样的,有不懂的可以私聊我
数据库怎么创建我就不教了,此处省略数据库(Mysql)安装,链接
SQL语句:
CREATE TABLE `tb_favorite` (
`id` bigint(16) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键',
`product_id` bigint(16) unsigned DEFAULT NULL COMMENT '商品编号',
`merchant_id` int(16) unsigned DEFAULT NULL COMMENT '商户编号',
`status` int(1) DEFAULT NULL COMMENT '状态:1-收藏,2-取消收藏',
`creator` varchar(32) DEFAULT NULL COMMENT '创建人',
`create_time` datetime DEFAULT NULL COMMENT '创建时间',
PRIMARY KEY (`id`),
UNIQUE KEY `merchant_id` (`merchant_id`,`product_id`)
) ENGINE=InnoDB AUTO_INCREMENT=46714 DEFAULT CHARSET=utf8 COMMENT='个人收藏';
model:实体
package com.xyy.model;
import java.io.Serializable;
import java.util.Date;
public class Favorite implements Serializable {
private static final long serialVersionUID = 1L;
/** 主键 */
private Long id;
/** 商品编号 */
private Long productId;
/** 商户编号 */
private Integer merchantId;
/** 状态:1-收藏,2-取消收藏 */
private Integer status;
/** 创建人 */
private String creator;
/** 创建时间 */
private Date createTime;
/**
*
* @return 返回数据库表tb_favorite的id字段值
*/
public Long getId() {
return id;
}
/**
* @param id 对应数据库表tb_favorite的id字段
*/
public void setId(Long id) {
this.id = id;
}
/**
*
* @return 返回数据库表tb_favorite的product_id字段值
*/
public Long getProductId() {
return productId;
}
/**
* @param productId 对应数据库表tb_favorite的product_id字段
*/
public void setProductId(Long productId) {
this.productId = productId;
}
/**
*
* @return 返回数据库表tb_favorite的merchant_id字段值
*/
public Integer getMerchantId() {
return merchantId;
}
/**
* @param merchantId 对应数据库表tb_favorite的merchant_id字段
*/
public void setMerchantId(Integer merchantId) {
this.merchantId = merchantId;
}
/**
*
* @return 返回数据库表tb_favorite的status字段值
*/
public Integer getStatus() {
return status;
}
/**
* @param status 对应数据库表tb_favorite的status字段
*/
public void setStatus(Integer status) {
this.status = status;
}
/**
*
* @return 返回数据库表tb_favorite的creator字段值
*/
public String getCreator() {
return creator;
}
/**
* @param creator 对应数据库表tb_favorite的creator字段
*/
public void setCreator(String creator) {
this.creator = creator;
}
/**
*
* @return 返回数据库表tb_favorite的create_time字段值
*/
public Date getCreateTime() {
return createTime;
}
/**
* @param createTime 对应数据库表tb_favorite的create_time字段
*/
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
}
dao:持久化层接口
package com.xyy.dao;
import org.apache.ibatis.annotations.Mapper;
import com.xyy.model.Favorite;
/**
* Favorite持久化层接口
* @ClassName: FavoriteMapper
* @author wujing
* @date 2017-07-13 15:09:50
*/
@Mapper
public interface FavoriteMapper{
Favorite selectByPrimaryKey(Long id);
}
mapper:持久化层映射文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.xyy.dao.FavoriteMapper" >
<resultMap id="BaseResultMap" type="com.xyy.model.Favorite" >
<id column="id" property="id" jdbcType="BIGINT" />
<result column="product_id" property="productId" jdbcType="BIGINT" />
<result column="merchant_id" property="merchantId" jdbcType="INTEGER" />
<result column="status" property="status" jdbcType="INTEGER" />
<result column="creator" property="creator" jdbcType="VARCHAR" />
<result column="create_time" property="createTime" jdbcType="TIMESTAMP" />
</resultMap>
<sql id="Base_Column_List" >
id, product_id, merchant_id, status, creator, create_time
</sql>
<sql id="Alias_Column_List" >
f.id, f.product_id, f.merchant_id, f.status, f.creator, f.create_time
</sql>
<insert id="insertSelective" parameterType="com.xyy.model.Favorite" useGeneratedKeys="true" keyProperty="id" keyColumn="id" >
insert into tb_favorite
<trim prefix="(" suffix=")" suffixOverrides="," >
<if test="productId != null" >
product_id,
</if>
<if test="merchantId != null" >
merchant_id,
</if>
<if test="status != null" >
status,
</if>
<if test="creator != null" >
creator,
</if>
<if test="createTime != null" >
create_time,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides="," >
<if test="productId != null" >
#{productId,jdbcType=BIGINT},
</if>
<if test="merchantId != null" >
#{merchantId,jdbcType=INTEGER},
</if>
<if test="status != null" >
#{status,jdbcType=INTEGER},
</if>
<if test="creator != null" >
#{creator,jdbcType=VARCHAR},
</if>
<if test="createTime != null" >
#{createTime,jdbcType=TIMESTAMP},
</if>
</trim>
</insert>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Long" >
delete from tb_favorite
where id = #{id,jdbcType=BIGINT}
</delete>
<delete id="batchDeleteByIds" parameterType="java.util.List" >
delete from tb_favorite
where id in
<foreach item="id" collection="list" open="(" separator="," close=")" >
#{id}
</foreach>
</delete>
<update id="updateByPrimaryKeySelective" parameterType="com.xyy.model.Favorite" >
update tb_favorite
<set >
<if test="productId != null" >
product_id = #{productId,jdbcType=BIGINT},
</if>
<if test="merchantId != null" >
merchant_id = #{merchantId,jdbcType=INTEGER},
</if>
<if test="status != null" >
status = #{status,jdbcType=INTEGER},
</if>
<if test="creator != null" >
creator = #{creator,jdbcType=VARCHAR},
</if>
<if test="createTime != null" >
create_time = #{createTime,jdbcType=TIMESTAMP},
</if>
</set>
where id = #{id,jdbcType=BIGINT}
</update>
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Long" >
select
<include refid="Base_Column_List" />
from tb_favorite
where id = #{id,jdbcType=BIGINT}
</select>
<select id="selectList" resultMap="BaseResultMap" parameterType="java.util.Map" >
select
<include refid="Base_Column_List" />
from tb_favorite
<where >
<if test="id != null" >
and id = #{id,jdbcType=BIGINT}
</if>
<if test="productId != null" >
and product_id = #{productId,jdbcType=BIGINT}
</if>
<if test="merchantId != null" >
and merchant_id = #{merchantId,jdbcType=INTEGER}
</if>
<if test="status != null" >
and status = #{status,jdbcType=INTEGER}
</if>
<if test="creator != null and creator != ''" >
and creator = #{creator,jdbcType=VARCHAR}
</if>
<if test="createTime != null" >
and create_time = #{createTime,jdbcType=TIMESTAMP}
</if>
</where>
<if test="property != null and property !=''" >
order by ${property} ${direction}
</if>
</select>
<select id="selectCount" resultType="java.lang.Integer" parameterType="java.util.Map" >
select count(1)
from tb_favorite
<where >
<if test="id != null" >
and id = #{id,jdbcType=BIGINT}
</if>
<if test="productId != null" >
and product_id = #{productId,jdbcType=BIGINT}
</if>
<if test="merchantId != null" >
and merchant_id = #{merchantId,jdbcType=INTEGER}
</if>
<if test="status != null" >
and status = #{status,jdbcType=INTEGER}
</if>
<if test="creator != null and creator != ''" >
and creator = #{creator,jdbcType=VARCHAR}
</if>
<if test="createTime != null" >
and create_time = #{createTime,jdbcType=TIMESTAMP}
</if>
</where>
</select>
</mapper>
service:业务层接口
package com.xyy.service;
import com.xyy.model.Favorite;
/**
* Favorite业务层接口
* @ClassName: FavoriteService
* @author wujing
* @date 2017-07-13 15:09:50
*/
public interface FavoriteService{
Favorite selectByPrimaryKey(Long id);
}
impl:业务层接口实现类
package com.xyy.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.xyy.dao.FavoriteMapper;
import com.xyy.model.Favorite;
import com.xyy.service.FavoriteService;
/**
* Favorite业务层实现类
* @ClassName: FavoriteServiceImpl
* @author wujing
* @date 2017-07-13 15:09:50
*/
@Transactional
@Service("favoriteService")
public class FavoriteServiceImpl implements FavoriteService {
@Autowired
private FavoriteMapper favoriteMapper;
public Favorite selectByPrimaryKey(Long id) {
return favoriteMapper.selectByPrimaryKey(id);
}
}
controller:控制层
package com.xyy.controller;
import java.util.HashMap;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import com.xyy.model.Favorite;
import com.xyy.service.FavoriteService;
/**
* Favorite控制层
* @ClassName: FavoriteController
* @author wujing
* @date 2017-07-13 15:09:50
*/
@RestController
@RequestMapping("/favorite")
public class FavoriteController{
private static final Logger LOGGER = LoggerFactory.getLogger(FavoriteController.class);
@Autowired
private FavoriteService favoriteService;
/**
* Favorite编辑
* @Title: update
* @param favorite 修改对象
* @return Object
* @author wujing
* @date 2017-07-13 15:09:50
*/
@RequestMapping("/detail/{id}")
public Object detail(@PathVariable Long id,ModelMap modelMap) {
Map<String,Object> resultMap = new HashMap<String,Object>();
try {
Favorite tempFavorite = favoriteService.selectByPrimaryKey(id);
resultMap.put("status", "success");
resultMap.put("data", tempFavorite);
} catch (Exception e) {
resultMap.put("status", "error");
resultMap.put("errorMsg", "系统异常");
LOGGER.error("Favorite添加异常", e);
}
return resultMap;
}
}
每个代码的具体实现和普通的spring项目代码没有区别,唯一的区别
1:pom.xml文件,需要加入依赖,具体如下:
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
mybatis-spring-boot-starter:mybatis与spring boot的依赖包
mysql-connector-java:数据库驱动
2:src/main/resources/application.properties需要加入数据库以及mybatis相关的配置,我比较中意key-value的方式,符合我们之前的习惯,yml的方式,就此忽略掉了,有需要了解的另行百度
注:一下配置根据自行情况更改,如跟着我做的,只需要更改datasource相关即可
#数据库配置文件
spring.datasource.url=jdbc:mysql://localhost:3306/xyy_v2?useUnicode=true&characterEncoding=utf8&allowMultiQueries=true
spring.datasource.username=admin
spring.datasource.password=admin
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#mybatis配置文件
mybatis.mapperLocations=classpath:com/xyy/mapper/*Mapper.xml
mybatis.type-aliases-package=com.xyy.model
同样右键运行main主入口,浏览器访问
自此第三节结束,第四节:spring boot整合freemark
还没有评论,来说两句吧...