springboot2.0 整合mybatis 梳理
最终效果图

第一步:创建springboot开发模板,选择2.0,需要选择的jar包有web,jdbc,mysql
第二步:编写配置文件,我习惯使用yml格式来配置
server:
port: 8081
spring:
datasource:
url: jdbc:mysql://localhost:3307/test
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
mybatis:
mapper-locations: classpath:mapper/*.xml
第三步:创建表
CREATE TABLE `student` (
`id` int(11) DEFAULT NULL,
`name` varchar(30) DEFAULT NULL,
`age` int(11) DEFAULT NULL,
`sex` varchar(20) DEFAULT NULL,
`telephone` int(11) NOT NULL,
`xueli` varchar(20) DEFAULT NULL,
PRIMARY KEY (`telephone`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
第四步:创建bean
package com.restart.bootlesson.bean; public class Student { private int id; private String name; private int age; private String sex; private int telephone; private String xueli; public int getId() { return id; } public void setId(int id) { this.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; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public int getTelephone() { return telephone; } public void setTelephone(int telephone) { this.telephone = telephone; } public String getXueli() { return xueli; } public void setXueli(String xueli) { this.xueli = xueli; } }
第五步:创建mapper类
package com.restart.bootlesson.mapper; import com.restart.bootlesson.bean.Student; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Select; import java.util.List; import java.util.Map; @Mapper public interface StudentMapper { // @Select("select * from student") public List<Student> query(Map<String,Object> param); }
第六步:创建mapper.xml文件,在resource下先建立一个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.restart.bootlesson.mapper.StudentMapper"> <resultMap type="com.restart.bootlesson.bean.Student" id="studentResult"> <result property="id" column="id" jdbcType="INTEGER"/> <result property="name" column="name" jdbcType="VARCHAR"/> <result property="age" column="age" jdbcType="INTEGER"/> <result property="sex" column="sex" jdbcType="VARCHAR"/> <result property="telephone" column="telephone" jdbcType="INTEGER"/> <result property="xueli" column="xueli" jdbcType="VARCHAR"/> </resultMap> <select id="query" parameterType="java.util.Map" resultMap="studentResult" > SELECT * FROM student; </select> </mapper>
第七步:创建service
package com.restart.bootlesson.service; import com.restart.bootlesson.bean.Student; import com.restart.bootlesson.mapper.StudentMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.util.List; import java.util.Map; @Service("studentService") @Transactional public class StudentService { @Autowired private StudentMapper studentMapper; public List<Student> query(Map<String, Object> param) { System.out.println("service"); return studentMapper.query(param); } }
第八步:创建controller
package com.restart.bootlesson.controller; import com.restart.bootlesson.bean.Student; import com.restart.bootlesson.mapper.StudentMapper; import com.restart.bootlesson.service.StudentService; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; import java.util.HashMap; import java.util.List; import java.util.Map; @RestController public class StartController { @Resource private StudentService studentService; @RequestMapping("/hello") public String hello(){ return "hello world"; } @RequestMapping("/query") public String query(){ Map<String, Object> param = new HashMap<>(); param.put("id",1); List<Student> list = studentService.query(param); Student student = list.get(0); return student.getName()+"--"+student.getSex(); } }
第九步:启动,访问,页面输出结果:
C--男
在配置过程中出现过几个问题,我觉得需要记录一下:
1.
org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.restart.bootlesson.mapper.StudentMapper.query at org.apache.ibatis.binding.MapperMethod$SqlCommand.<init>(MapperMethod.java:225) ~[mybatis-3.4.5.jar:3.4.5]
关于这个异常,在网上着了 很多答案,但是各执一词,提供的方案也未必管用.
这个异常的原因是在访问时,没有找到mapper对应的配置文件.之所以会出现这个问题,是因为在最开始配置时,是因为没有指定mybatis相关配置.
有二种解决方案:
第一种就是按照我现在的配置:对mybatis进行指定要扫描的位置
server:
port: 8081
spring:
datasource:
url: jdbc:mysql://localhost:3307/test
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
mybatis:
mapper-locations: classpath:mapper/*.xml
一开始我并没有配置这个,所以报找不到对应的方法这个异常.
第二种解决方案是添加mybatis-config.xml,在这个配置文件中指定每个mapper类映射的配置文件,这个方案我没有验证,因为这种解决思路其实和上面的殊途同归.
网上还有一种更改pom.xml文件的做法,在里边配置
<build> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> </includes> </resource> <resource> <directory>src/main/resources</directory> </resource> </resources> </build>
这种做法是用来解决maven在编译时不把xml文件编译到class中这种情况的,后来经过验证,如果按照方案1的配置,即便pom.xml不配置这些,也仍然能够跑通.
2.
Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.
2018-03-11 12:48:21.317 ERROR 453992 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :
***************************
APPLICATION FAILED TO START
***************************
Description:
Field studentMapper in com.restart.bootlesson.service.StudentService required a bean of type 'com.restart.bootlesson.mapper.StudentMapper' that could not be found.
Action:
Consider defining a bean of type 'com.restart.bootlesson.mapper.StudentMapper' in your configuration.
这个错误的原因是我一开始虽然自动注入了mapper,但是spring没有获取到这个bean,由于没找到,所以无法启动.
两个解决方案:
第一种:@Mapper
@Mapper public interface StudentMapper { // @Select("select * from student") public List<Student> query(Map<String,Object> param); }
在这个mapper类上边添加@mapper注解,就是告诉spring这个类是一个mapper
第二种:@MapperScan
@SpringBootApplication @MapperScan("com.restart.bootlesson.mapper") public class BootLessonApplication { public static void main(String[] args) { SpringApplication.run(BootLessonApplication.class, args); } }
在启动类上添加这个注解,并制定要扫描的mapper类所在位置,作用和@Mapper是一样的.
特别注意的是,springboot对目录层级是有要求的.启动类必须在所有controller,service,mapper所在目录的最外顶层.spring会按照启动类所在包位置逐级扫描.

浙公网安备 33010602011771号