spring boot 整合mybatis
1.打开idea创建一个项目
2.在弹出的窗口中选择spring initializr(初始化项目),点击next
3.接下来填写group 与artifact
(groupid和artifactId被统称为“坐标”是为了保证项目唯一性而提出的,如果你要把你项目弄到maven本地仓库去,你想要找到你的项目就必须根据这两个id去查找。
groupId一般分为多个段,这里我只说两段,第一段为域,第二段为公司名称。域又分为org、com、cn等等许多,其中org为非营利组织,com为商业组织。举个apache公司的tomcat项目例子:这个项目的groupId是org.apache,它的域是org(因为tomcat是非营利项目),公司名称是apache,artigactId是tomcat。
比如我创建一个项目,我一般会将groupId设置为cn.dbc,cn表示域为中国,dbc是我个人姓名缩写,artifactId设置为testProj,表示你这个项目的名称是testProj,依照这个设置,你的包结构最好是cn.pq.testProj打头的,如果有个StudentDeng,它的全路径就是cn.pq.testProj.StudentDeng)
4.接着选择基本依赖项,也就是基础jar包
5.接下来页面继续点击next即可完成创建项目
6.建立基本包类结构进行测试,包括pom文件
package com.doublebc.common.domain.student; Student类 /** * @author DBC * @date create 2019/2/28 12:20 */ public class Student { private Integer id; private String name; private String sex; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } }
package com.doublebc.common.student.persistence; import com.doublebc.common.domain.student.Student; /** * @author DBC * @date create 2019/2/28 12:14 */ public interface StudentMapper { void addStudent(Student student); }
package com.doublebc.common.student.repository; import com.doublebc.common.student.persistence.StudentMapper; import com.doublebc.common.domain.student.Student; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Repository; /** * @author DBC * @date create 2019/2/28 12:15 */ @Repository public class StudentRepository { @Autowired private StudentMapper studentMapper; public void addStudent(Student student){ this.studentMapper.addStudent(student); } }
package com.doublebc.common; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication @MapperScan("com.doublebc") public class CommonApplication { public static void main(String[] args) { SpringApplication.run(CommonApplication.class, args); } }
<?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.doublebc.common.student.persistence.StudentMapper"> <insert id="addStudent" useGeneratedKeys="true" keyProperty="id"> insert into T_STUDENT ( name, sex )values ( #{name}, #{sex} ) </insert> </mapper>
属性文件 server.port=8019 spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC spring.datasource.username=root spring.datasource.password=12345 spring.datasource.driver-class-name=com.mysql.jdbc.Driver
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.3.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.doublebc</groupId> <artifactId>common</artifactId> <version>0.0.1-SNAPSHOT</version> <name>common</name> <description>common project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <!-- 持久层 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.0.0</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> <!--编译时增加xml文件--> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> </includes> <filtering>true</filtering> </resource> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> </resource> </resources> </build> </project>
问题一:org.apache.ibatis.binding.BindingException: Invalid bound statement (not found):异常
1:检查xml文件所在的package名称是否和interface对应的package名称一一对应
2:检查xml文件的namespace是否和xml文件的package名称一一对应
3:检查函数名称能否对应上
4:去掉xml文件中的中文注释
5:随意在xml文件中加一个空格或者空行然后保存
如果以上没问题,那么在pom文件中加上红色那一段,然后build重新编译一下项目,看xml文件是否被编译成功,并且检查是否与mapper.class字节码文件在同一包下。
如果在target目录下有xml,但与mapper.class又没在同一目录下,可包名明明是相同的啊?如何解决?
注意:我们在resources目录下建包结构的时候需要一层一层建,或者用“/”隔开,不能用点“.”来分隔多个目录。
所以符合以上规则重新建立一个与Mapper.java同路径的包名,即可正确编译。
问题二:在建包的时候,使用点“.”与“/”符合的区别.
在java包中则不可用/来建包结构,可以用点“.”
例如:文件的相对路径为com/demo/student与com/demo.student的区别
这样应该就没问题啦!
附上结果: