目录
一、添加maven依赖
mybatis-spring
<properties> <druid-version>1.0.29</druid-version> <mybatis-spring-version>1.3.1</mybatis-spring-version> </properties>
<!-- 配置Druid数据源--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>${druid-version}</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <!-- mybatis集成 --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>${mybatis-spring-version}</version> </dependency>
二、指定entity,dao,mapper等
当然也可以手动写User,UserMapper,UserMapper.xml,mybatis-config.xml
三、添加注解(贴出源代码)
项目目录结构
application.properties属性文件(指定数据源druid,数据库连接信息,mybatis配置文件位置)
# 使用druid数据源 spring.datasource.type: com.alibaba.druid.pool.DruidDataSource #mybatis配置信息 spring.datasource.driverClassName = com.mysql.jdbc.Driver spring.datasource.url = jdbc:mysql://***:3306/***?useUnicode=true&characterEncoding=utf-8 spring.datasource.username = *** spring.datasource.password = *** #指定mybatis配置文件位置 mybatis.config-locations=classpath:mybatis/mybatis-config.xml mybatis.mapper-locations=classpath:mapper/*.xml
mybatis-config.xml必须手动修改
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <settings> <setting name="mapUnderscoreToCamelCase" value="true"/> </settings> <typeAliases> <typeAlias type="com.zk.kfcloud.Entity.web.User" alias="User"/> </typeAliases> <mappers> <mapper resource="/mapper/UserMapper.xml"/> </mappers> </configuration>
UserMapper.xml(我是使用Mybatis-Generator自动生成的,注意namespace换成你自己的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.zk.kfcloud.Dao.UserMapper" > <resultMap id="BaseResultMap" type="com.zk.kfcloud.Entity.web.User" > <id column="user_id" property="userId" jdbcType="INTEGER" /> <result column="loginname" property="loginname" jdbcType="VARCHAR" /> <result column="password" property="password" jdbcType="VARCHAR" /> <result column="username" property="username" jdbcType="VARCHAR" /> <result column="rights" property="rights" jdbcType="VARCHAR" /> <result column="status" property="status" jdbcType="BIT" /> <result column="role_id" property="roleId" jdbcType="INTEGER" /> <result column="last_login" property="lastLogin" jdbcType="TIMESTAMP" /> <result column="parent_id" property="parentId" jdbcType="INTEGER" /> <result column="open_id" property="openId" jdbcType="VARCHAR" /> </resultMap> <sql id="Base_Column_List" > user_id, loginname, password, username, rights, status, role_id, last_login, parent_id, open_id </sql> <select id="findAllUsers" resultMap="BaseResultMap" parameterType="java.lang.Integer" > select <include refid="Base_Column_List" /> from tb2_user </select> <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" > select <include refid="Base_Column_List" /> from tb2_user where user_id = #{userId,jdbcType=INTEGER} </select> <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" > delete from tb2_user where user_id = #{userId,jdbcType=INTEGER} </delete> <insert id="insert" parameterType="com.zk.kfcloud.Entity.web.User" > insert into tb2_user (user_id, loginname, password, username, rights, status, role_id, last_login, parent_id, open_id) values (#{userId,jdbcType=INTEGER}, #{loginname,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR}, #{username,jdbcType=VARCHAR}, #{rights,jdbcType=VARCHAR}, #{status,jdbcType=BIT}, #{roleId,jdbcType=INTEGER}, #{lastLogin,jdbcType=TIMESTAMP}, #{parentId,jdbcType=INTEGER}, #{openId,jdbcType=VARCHAR}) </insert> <insert id="insertSelective" parameterType="com.zk.kfcloud.Entity.web.User" > insert into tb2_user <trim prefix="(" suffix=")" suffixOverrides="," > <if test="userId != null" > user_id, </if> <if test="loginname != null" > loginname, </if> <if test="password != null" > password, </if> <if test="username != null" > username, </if> <if test="rights != null" > rights, </if> <if test="status != null" > status, </if> <if test="roleId != null" > role_id, </if> <if test="lastLogin != null" > last_login, </if> <if test="parentId != null" > parent_id, </if> <if test="openId != null" > open_id, </if> </trim> <trim prefix="values (" suffix=")" suffixOverrides="," > <if test="userId != null" > #{userId,jdbcType=INTEGER}, </if> <if test="loginname != null" > #{loginname,jdbcType=VARCHAR}, </if> <if test="password != null" > #{password,jdbcType=VARCHAR}, </if> <if test="username != null" > #{username,jdbcType=VARCHAR}, </if> <if test="rights != null" > #{rights,jdbcType=VARCHAR}, </if> <if test="status != null" > #{status,jdbcType=BIT}, </if> <if test="roleId != null" > #{roleId,jdbcType=INTEGER}, </if> <if test="lastLogin != null" > #{lastLogin,jdbcType=TIMESTAMP}, </if> <if test="parentId != null" > #{parentId,jdbcType=INTEGER}, </if> <if test="openId != null" > #{openId,jdbcType=VARCHAR}, </if> </trim> </insert> <update id="updateByPrimaryKeySelective" parameterType="com.zk.kfcloud.Entity.web.User" > update tb2_user <set > <if test="loginname != null" > loginname = #{loginname,jdbcType=VARCHAR}, </if> <if test="password != null" > password = #{password,jdbcType=VARCHAR}, </if> <if test="username != null" > username = #{username,jdbcType=VARCHAR}, </if> <if test="rights != null" > rights = #{rights,jdbcType=VARCHAR}, </if> <if test="status != null" > status = #{status,jdbcType=BIT}, </if> <if test="roleId != null" > role_id = #{roleId,jdbcType=INTEGER}, </if> <if test="lastLogin != null" > last_login = #{lastLogin,jdbcType=TIMESTAMP}, </if> <if test="parentId != null" > parent_id = #{parentId,jdbcType=INTEGER}, </if> <if test="openId != null" > open_id = #{openId,jdbcType=VARCHAR}, </if> </set> where user_id = #{userId,jdbcType=INTEGER} </update> <update id="updateByPrimaryKey" parameterType="com.zk.kfcloud.Entity.web.User" > update tb2_user set loginname = #{loginname,jdbcType=VARCHAR}, password = #{password,jdbcType=VARCHAR}, username = #{username,jdbcType=VARCHAR}, rights = #{rights,jdbcType=VARCHAR}, status = #{status,jdbcType=BIT}, role_id = #{roleId,jdbcType=INTEGER}, last_login = #{lastLogin,jdbcType=TIMESTAMP}, parent_id = #{parentId,jdbcType=INTEGER}, open_id = #{openId,jdbcType=VARCHAR} where user_id = #{userId,jdbcType=INTEGER} </update> </mapper>
UserMapper(添加两个注解@Component,@Mapper)
package com.zk.kfcloud.Dao; import com.zk.kfcloud.Entity.web.User; import org.apache.ibatis.annotations.Mapper; import org.springframework.stereotype.Component; import java.util.List; @Mapper @Component public interface UserMapper { int deleteByPrimaryKey(Integer userId); int insert(User record); int insertSelective(User record); User selectByPrimaryKey(Integer userId); int updateByPrimaryKeySelective(User record); int updateByPrimaryKey(User record); List<User> findAllUsers(); }
建议将这里的@Mapper去掉(只留下@Component),然后在启动程序中添加@MapperScan
package com.zk.kfcloud.Controller; import com.zk.kfcloud.Dao.UserMapper; import com.zk.kfcloud.Entity.web.User; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import java.util.List; @RestController public class testMyBatis { @Autowired private UserMapper userMapper; @GetMapping("/findAll") List<User> findall(){ List<User> allUsers = userMapper.findAllUsers(); System.out.println(allUsers); return allUsers; } }
四、效果展示
- 浏览器接受json数据(因为controller中使用了@RestController)