在之前介绍过快速搭建一个Springboot程序,那么这篇就记录下用Springboot整合mybatis的一些小坑坎。
在resources下的sql脚本复制。在navicat等sql执行(先创建表)
pom.xml加入编译配置(默认只编译resources的xml文件,不配置无法编译dao里的xml,这点和eclipse系列不太一样)
src创建对应文件夹。在application.properties中声明dao所在的包和mybatis映射pojo引用的包
基于xml/注解两种方式实现mbatis的增删改查。dao的interface要加上@Mapper,注入的话加上@Autuwired,如果基于xml的注意格式关联;
对于大部分和普通的都是没有关联的映射,那么pojo对象直接对应数据库的各个字段即可。但是如果遇到一对一,或者一对多的映射,大致的处理方式有两种
1.联表查询,把所有字段都查询出来,然后在自定义的Map中配置各个对象属性。
2. 嵌套查询,先查询1查第一个大对象,在大对象配置到pojo对象的时候再调用查询2(带上查询1查到的参数)
对于一对一映射和一对多映射。最大的区别就是配置名称了。对于一对一的pojo对象
总的来说,Springboot的主要配置只有两个地方——pom.xml和application.properties(有的是yml).pom.xml主要配置为jar包的管理和编译的一些配置。而application中主要是关于一些属性管理的配置。类似数据库或者一些量数值,在我整合的时候遇到了一下几个坎:
其中刚开始一直很坑的地方就是properties的配置问题。主要的问题就是数据库版本问题。我刚开始的配置是8.0.11版本的jar包。我的数据库也是8版本的数据库,但是更改驱动这些改了这个错就出现那个错。后来实在烦就把我的8版本的数据库当成5版本的用,把项目中所有的有关数据库的配置改成5版本的。但是注意的是(在配置数据库不要加到6版本以上的后缀,那么这个版本的driver会识别不了对应参数也会报错。)
<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.0modelVersion>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.1.4.RELEASEversion>
<relativePath/>
parent>
<groupId>comgroupId>
<artifactId>mybatisartifactId>
<version>0.0.1-SNAPSHOTversion>
<name>mybatisname>
<description>Demo project for Spring Bootdescription>
<properties>
<java.version>1.8java.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
<version>2.0.1version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<scope>runtimescope>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
dependencies>
<build>
<resources>
<resource>
<directory>src/main/resourcesdirectory>
resource>
<resource>
<directory>src/main/javadirectory>
<includes>
<include>**/*.xmlinclude>
includes>
<filtering>truefiltering>
resource>
resources>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
plugins>
build>
project>
创建dog和user类(配合自己数据库的都行)
package com.mybatis.pojo;
public class dog {
private String dogname;
private String dogtype;
private user master;
public String getDogname() {
return dogname;
}
public void setDogname(String dogname) {
this.dogname = dogname;
}
public String getDogtype() {
return dogtype;
}
public void setDogtype(String dogtype) {
this.dogtype = dogtype;
}
public user getMaster() {
return master;
}
public void setMaster(user master) {
this.master = master;
}
}
package com.mybatis.pojo;
import java.util.List;
public class user {
private String username;
private String password;
private List<dog> dogs;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public List<dog> getDogs() {
return dogs;
}
public void setDogs(List<dog> dogs) {
this.dogs = dogs;
}
}
两个mapper:规则和ssm的传统项目差不多
package com.mybatis.dao;
import com.mybatis.pojo.dog;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import java.util.List;
@Mapper
public interface dogMapper {
@Select("select * from dog")//不做配置情况找不到master,只能找数据库对应的字段
List<dog>getalldog();
//如果两个以上参数需要加param
@Select("select * from dog where dogname=#{name} and dogtype=#{type}")
List<dog>getdogbynametype(@Param("name")String dogname,@Param("type")String dogtype);
List<dog> getdogbyType(String type);
}
<mapper namespace="com.mybatis.dao.dogMapper" >
<resultMap id="dog" type="dog">
<result property="dogname" column="dogname"/>
<result property="dogtype" column="dogtype"/>
<association property="master" javaType="user">
<result property="username" column="username"/>
<result property="password" column="password"/>
association>
resultMap>
<select id="getdogbyType" resultMap="dog">
select a.dogname,a.dogtype ,b.username,b.password
from dog a,user b
where a.master_name=b.username
and a.dogtype=#{type}
select>
mapper>
userMapper.java
package com.mybatis.dao;
import com.mybatis.pojo.user;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.util.List;
@Mapper
public interface userMapper {
@Select("select * from user")
List<user> getalluser();
List<user> getalluserwithdog();
}
userMapper.xml
<mapper namespace="com.mybatis.dao.userMapper" >
<select id="getalluserwithdog" resultMap="user1">
select * from user
select>
<resultMap id="user1" type="user">
<result property="username" column="username"/>
<result property="password" column="password"/>
<collection property="dogs" ofType="com.mybatis.pojo.dog" column="username" select="getdogbyusername">collection>
resultMap>
<select id="getdogbyusername" resultType="com.mybatis.pojo.dog">
select * from dog where master_name=#{username}
select>
mapper>
controller:(restcontroller就可以省掉@Responsebody注解)
package com.mybatis.controller;
import com.mybatis.dao.dogMapper;
import com.mybatis.dao.userMapper;
import com.mybatis.pojo.dog;
import com.mybatis.pojo.user;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class DogController {
@Autowired(required = false)
private dogMapper dogMapper;
@GetMapping("getalldog")
public List<dog> getalldog()
{
return dogMapper.getalldog();
}
@GetMapping("getdogbymaster")//不加参数默认为bigsai
public List<dog> getalldog(@RequestParam(value = "type",defaultValue = "舔狗")String type)
{
return dogMapper.getdogbyType(type);
}
}
TestController
package com.mybatis.controller;
import com.mybatis.dao.dogMapper;
import com.mybatis.dao.userMapper;
import com.mybatis.pojo.dog;
import com.mybatis.pojo.user;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class TestController {
@Autowired(required = false)
private userMapper userMapper;
@Autowired(required = false)
private dogMapper dogMapper;
@GetMapping("getalluser")//普通情况,假设user只有账户密码。返回的只有账户密码 dog为null
public List<user> getalluer()
{
return userMapper.getalluser();
}
@GetMapping("getdogbyType")//一对一映射,一个狗有个主人 返回该type
public List<dog>gedogbyType(@RequestParam(value = "type",defaultValue = "舔狗")String type)
{
return dogMapper.getdogbyType(type);
}
@GetMapping("getalluserwithdog")
public List<user>getalluserwithdog()
{
return userMapper.getalluserwithdog();
}
}
还有最重要的就是application.properties的配置:(maven继承了一些插件被我注释掉没有用)
server.port=8080
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/bigsai?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
spring.datasource.password=123456
spring.datasource.username=root
mybatis.mapper-locations=classpath:com/mybatis/dao/*.xml
mybatis.type-aliases-package=com.mybatis.pojo
## 骆驼峰 数据库为xx_name pojo为xxName
mybatis.configuration.map-underscore-to-camel-case=true
执行main函数启动Springboot。整合完毕!
就这样就整合成功了。?
本文如果有错请指正,配置前看了一些大佬的配置学来的。没有太多技术含量纯属笔记用!
后端、爬虫、数据结构算法
等感性趣欢迎关注我的个人公众号交流:bigsai