1.建立Maven工程
2.建立appliaction.properties
加入mybatis-config.xml配置及掃描mapper.xml文件夾配置
3.把mybatis工程中寫好的mappe接口,mapper的xml文件,mybatis-config.xml以及實體類拷貝過來
4.Pom文件修改
1)log4j的依賴只留Log4j其餘兩個刪除
2)加入sprongBoot以及mybatis-Springboot依賴包
3)mybatis-java驅動確認
2)要加上紅框的内容
不然啓動時會出現Unsatisfied dependency expressed through bean property 'sqlSessionFactory'錯誤
5.編寫啓動類
---------------------------------------------------------------
package mybatis.simple;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan(basePackages = {"mybatis.simple.mapperInterface"})
public class MybatisSpringbootApp {
public static void main(String[] args) {
SpringApplication.run(MybatisSpringbootApp.class, args);
}
}
---------------------------------------------------------------
6.編寫Controler
----------------------------------------------------------------
package mybatis.simple.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import mybatis.simple.mapper.UserMapper;
import mybatis.simple.model.SysUser;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
@RestController
@RequestMapping("/")
public class UserController {
@Resource
private UserMapper userMapper;
@GetMapping("getOne")
public SysUser selectByUserId(@RequestParam(value = "id") Long id){
return userMapper.selectById(id);
}
@GetMapping("getAll")
public List<SysUser> selectByUserId(){
return userMapper.selectAll();
}
@RequestMapping(value = "/deleteById")
@ResponseBody
public List<SysUser> getBookDetails(HttpServletRequest request) {
String userId = request.getParameter("id");
SysUser sysUser = new SysUser();
sysUser.setId(Long.valueOf(userId));
userMapper.deleteById(sysUser);
return userMapper.selectAll();
}
}
------------------------------------------------------------
Pom.xml文件内容
--------------------------------------------------------------------------------------------------
<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>
<groupId>com.accenture</groupId>
<artifactId>mybatisSpringboot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>myBatis和springboot整合</name>
<description>myBatis和springboot整合</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<!-- Spingboot相关jar包版本 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
</parent>
<dependencies>
<!-- Springboot核心jar包 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!-- web开发包:包含Tomcat和Springmvc -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- spring-boot热部署 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<!-- Spring Boot Mybatis 依赖 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.2.0</version>
</dependency>
<dependency>
<!-- 单元测试 -->
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<!-- 表示开发的时候引入,发布的时候不会加载此包 -->
<scope>test</scope>
</dependency>
<!-- mybatis依赖包 -->
<!-- dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId>
<version>3.4.6</version> </dependency -->
<!-- mysql数据库依赖 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.11</version>
</dependency>
<!-- 日志依赖包 -->
<!--dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId>
<version>1.7.12</version> </dependency> <dependency> <groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId> <version>1.7.12</version> </dependency -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
<!-- 添加资源 -->
<resources>
<resource>
<directory>src/main/resources</directory>
<!-- src/main/resources下的指定资源放行 -->
<includes>
<include>**/*.properties</include>
<include>**/*.yml</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
</project>
-----------------------------------------------------------------------------------------------------------
mybatis-config.xml配置内容
----------------------------------------------------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- 配置指定使用LOG4J输出日志 -->
<settings>
<setting name="logImpl" value="LOG4J"/>
</settings>
<!-- 配置包的别名,这样我们在mapper中定义时,就不需要使用类的全限定名称,只需要使用类名即可 -->
<typeAliases>
<package name="mybatis.simple.model"/>
</typeAliases>
<!-- 数据库配置 -->
<!-- environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8&useSSL=false&allowPublicKeyRetrieval=true"/>
<property name="username" value="root"/>
<property name="password" value="123"/>
</dataSource>
</environment>
</environments-->
<!-- mybatis的SQL语句和映射配置文件 -->
<!-- mappers>
<package name="mybatis.simple.mapper"/>
</mappers-->
</configuration>
-------------------------------------------------------------------------------------
application.properties配置内容
------------------------------------------------------------------------------
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/mybatis?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8&useSSL=false&allowPublicKeyRetrieval=true
spring.datasource.username=root
spring.datasource.password=123
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
mybatis.config-location=classpath:mybatis-config.xml
mybatis.mapperLocations=classpath:mybatis/simple/mapper/*.xml
#mybatis.typeAliasesPackage=mybatis.simple.model
---------------------------------------------------------------------------------------