1、pom.xml 添加mybatis和mysql依赖
<!-- 添加 MyBatis --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.2.0</version> </dependency> <!-- 添加 MySQL --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.41</version> </dependency>
2、application.properties 里面配置数据库信息
#数据库数据源配置 spring.datasource.url=jdbc:mysql://localhost/demo?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true spring.datasource.username=root spring.datasource.password=root spring.datasource.driver-class-name=com.mysql.jdbc.Driver
3、配置mybatis,有两种方式,一种用注解,一种用xml,一般习惯用XML。
- 用注解
@Mapper public interface UserMapper { /** * 查询所有用户 * @return */ @Select("select id,name,age FROM tb_tic_user") List<UserDo> selectAll(); }
public class UserDo implements Serializable{ private static final long serialVersionUID = -7488908967791971359L; private int Id; private String name; private int age; public int getId() { return Id; } public void setId(int id) { Id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "UserDo{" + "Id=" + Id + ", name=‘" + name + ‘\‘‘ + ", age=" + age + ‘}‘; } }
@Controller public class IndexController { @Autowired UserMapper userMapper; @RequestMapping("/selectAll") public @ResponseBody List<UserDo> selectAll(){ return userMapper.selectAll(); } }
启动不报错,用注解配置成功,主要是在Mapper类上增加@Mapper注解,sql语句用注解放在方法上面。配置简单,但是会有重复代码,不推荐。
- 用xml配置,增加配置类用来扫描mapper接口
application.properties 中增加mybatis配置
#mybatis配置 mybatis.mapper-locations=classpath:mapper/*.xml mybatis.type-aliases-package=com.example.demo.domain
@Configuration @MapperScan(value = "com.example.demo.mapper") public class MybatisConfig { }
package com.example.demo.mapper; import com.example.demo.domain.Test; public interface TestMapper { int deleteByPrimaryKey(Integer id); int insert(Test record); int insertSelective(Test record); Test selectByPrimaryKey(Integer id); int updateByPrimaryKeySelective(Test record); int updateByPrimaryKey(Test record); }
<?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.example.demo.mapper.TestMapper" > <resultMap id="BaseResultMap" type="com.example.demo.domain.Test" > <id column="ID" property="id" jdbcType="INTEGER" /> <result column="NAME" property="name" jdbcType="VARCHAR" /> </resultMap> <sql id="Base_Column_List" > ID, NAME </sql> <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" > select <include refid="Base_Column_List" /> from tb_tic_test where ID = #{id,jdbcType=INTEGER} </select> <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" > delete from tb_tic_test where ID = #{id,jdbcType=INTEGER} </delete> <insert id="insert" parameterType="com.example.demo.domain.Test" > insert into tb_tic_test (ID, NAME) values (#{id,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR}) </insert> <insert id="insertSelective" parameterType="com.example.demo.domain.Test" > insert into tb_tic_test <trim prefix="(" suffix=")" suffixOverrides="," > <if test="id != null" > ID, </if> <if test="name != null" > NAME, </if> </trim> <trim prefix="values (" suffix=")" suffixOverrides="," > <if test="id != null" > #{id,jdbcType=INTEGER}, </if> <if test="name != null" > #{name,jdbcType=VARCHAR}, </if> </trim> </insert> <update id="updateByPrimaryKeySelective" parameterType="com.example.demo.domain.Test" > update tb_tic_test <set > <if test="name != null" > NAME = #{name,jdbcType=VARCHAR}, </if> </set> where ID = #{id,jdbcType=INTEGER} </update> <update id="updateByPrimaryKey" parameterType="com.example.demo.domain.Test" > update tb_tic_test set NAME = #{name,jdbcType=VARCHAR} where ID = #{id,jdbcType=INTEGER} </update> </mapper>
@Controller public class IndexController { @Autowired TestMapper testMapper; @RequestMapping("/test") public @ResponseBody Test test(){ return testMapper.selectByPrimaryKey(1); } }
习惯用xml,这样方便把基础方法提出来