1、新建springboot项目,结构如下
2、pom.xml新增mybatis依赖
<dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.2</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency>
3、application.properties配置文件代码如下
值得注意的地方为需要指出Mapper.xml的配置路径mybatis.mapperLocations=classpath:MytestMapper.xml
spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://127.0.0.1/demo?characterEncoding=GBK&useSSL=false spring.datasource.username=root spring.datasource.password=123456 pring.datasource.druid.test-on-borrow=true spring.datasource.druid.test-while-idle=true mybatis.mapperLocations=classpath:MytestMapper.xml
MytestMapper.java文件
package com.example.demo.dao; import com.example.demo.object.Mytest; import org.apache.ibatis.annotations.Mapper; import java.util.List; @Mapper public interface MytestMapper { List<Mytest> getMytestList(); void addMtest(Mytest mytest); void updateMytest(Mytest mytest); void deleteMytest(Mytest mytest); }
Mytest.java文件
package com.example.demo.object; import lombok.Data; @Data public class Mytest { int id; String name; int age; int height; }
MytestMapper.xml
<?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.dao.MytestMapper"> <!-- <resultMap id="BaseResultMap" type="com.example.demo.object.Mytest">--> <!-- <result column="id" jdbcType="DECIMAL" property="id" />--> <!-- <result column="name" jdbcType="VARCHAR" property="name" />--> <!-- <result column="age" jdbcType="DECIMAL" property="age" />--> <!-- <result column="height" jdbcType="DECIMAL" property="height" />--> <!-- </resultMap>--> <!-- <select id="getMytestList" resultType="com.example.demo.object.Mytest">--> <!-- select id as id, name as name, age as age,height as height from mytest--> <!-- </select>--> <select id="getMytestList" resultType="com.example.demo.object.Mytest"> select* from mytest </select> <insert id="addMtest" parameterType="com.example.demo.object.Mytest" > insert into mytest (id,name,age,height) values (#{id}, #{name}, #{age},#{height}) </insert> <update id="updateMytest" parameterType="com.example.demo.object.Mytest"> update mytest set name=#{name}, age=#{age}, height=#{height} where id=#{id} </update> <delete id="deleteMytest" parameterType="com.example.demo.object.Mytest"> delete from mytest where id=#{id} </delete> <!-- <select id="getMytestList" resultMap="BaseResultMap">--> <!-- select id,name,age,height from demo.mytest--> <!-- </select>--> </mapper>
测试类代码:
@SpringBootTest class DemoApplicationTests { @Resource MytestMapper mytestMapper; @Test void contextLoads() { List<Mytest> list = mytestMapper.getMytestList(); System.out.println(list); } @Test void intertR() { Mytest mytest = new Mytest(); mytest.setId(3); mytest.setName("ding"); mytest.setAge(15); mytest.setHeight(160); mytestMapper.addMtest(mytest); } @Test void updateR() { Mytest mytest = new Mytest(); mytest.setId(3); mytest.setName("ding"); mytest.setAge(15); mytest.setHeight(180); mytestMapper.updateMytest(mytest); } @Test void deleteR() { Mytest mytest = new Mytest(); mytest.setId(3); mytestMapper.deleteMytest(mytest); } }