springboot 整合 mybatis 开发
1.springboot项目能正常访问
2. pom.xml配置
<?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.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.study</groupId>
<artifactId>cdmy</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>cdmy</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--web项目-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 数据源 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.6</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.31</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.4.5</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.2</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.0</version>
</dependency>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.23</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.3.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.6</version>
</dependency>
<dependency>
<groupId>javax.transaction</groupId>
<artifactId>javax.transaction-api</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.0.1.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>persistence-api</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.20</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.20</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
</build>
</project>
- springboot 的application.yml配置
需要注意声明的是,这个yml配置文件中的注释特别注意,如果加了中文注释,程序起来的时候,可能报错
server:
port: 52000
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driverClassName: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/test?autoReconnect=true&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&serverTimezone=PRC&useSSL=false
username: root
password: 123456
initialSize: 1
minIdle: 3
maxActive: 20
maxWait: 60000
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 30000
validationQuery: select 'x'
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
maxPoolPreparedStatementPerConnectionSize: 20
filters: stat,wall,slf4j
connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
mybatis:
mapper-locations: classpath:com/study/cdmy/dao/mapping/*Mapper.xml
type-aliases-package: com.study.cdmy
logging:
com.study.cdmy
level
debug
- 编写代码 user类为例子
UserMapper.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.study.cdmy.dao.UserMapper">
<!---通用查询结果列-->
<sql id="all_column">
o.id, o.username, o.age
</sql>
<!---通用查询结果列-->
<sql id="all_where">
<if test="id != null ">
and o.id = #{id}
</if>
<if test="username != null and username != '' ">
and o.username = #{username}
</if>
<if test="age != null ">
and o.age = #{age}
</if>
</sql>
<!-- 插入一条记录,添加 (匹配有值的字段)-->
<insert id="insert">
INSERT INTO user
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="id != null">
id,
</if>
<if test="username != null">
username,
</if>
<if test="age != null">
age,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="id!=null">
#{id},
</if>
<if test="username!=null">
#{username},
</if>
<if test="age!=null">
#{age},
</if>
</trim>
</insert>
<!-- 实体类与数据库字段映射-->
<resultMap type="com.study.cdmy.entity.User" id="UserResultMap">
<!-- 用id属性来映射主键字段 -->
<id property="id" column="id"/> <!-- 用result属性来映射非主键字段 -->
<result property="username" column="username"/>
<result property="age" column="age"/>
</resultMap>
</mapper>
UserMapper.java
package com.study.cdmy.dao;
import com.study.cdmy.entity.User;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
/**
*
* -数据库操作接口层
*
**/
@Mapper
@Repository
public interface UserMapper{
public int insert( User entity);
}
UserService.java
package com.study.cdmy.service;
import com.study.cdmy.entity.User;
import com.study.cdmy.dao.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
*
* -业务逻辑处理层
*
**/
@Service
public class UserService{
@Autowired
UserMapper userMapper;
public Integer insert(String username,Integer age){
User user =new User(username,age);
Integer ok = userMapper.insert(user);
return ok;
}
}
UserController.java
package com.study.cdmy.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.study.cdmy.service.UserService;
/**
* 控制层
* Created by cd
*/
@RestController
@RequestMapping(value = "/test",method = RequestMethod.POST)
public class UserController{
@Autowired
UserService userService;
@RequestMapping(value = "test")
public String test(){
return "success";
}
/**
*
* @param username
* @param age
* @return
*/
@RequestMapping(value = "insert")
public String insert(String username,Integer age){
Integer insert = userService.insert(username, age);
if(insert >0){
return "插入成功";
}else {
return "插入失败";
}
}
}
- 启动项目
然后测试
插入成功
如果出现Invalid bound statement (not found):这个异常,看看自己的yml配置文件里面的mybatis:
mapper-locations: 路径有没有写错
源码请看码云:springboot整合mabatis 分支springboot整合mabatis