spring集成mybatis
1.资源文件
pom文件
<?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 https://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.3.4.RELEASE
</version>
<relativePath/>
</parent>
<groupId>com.example
</groupId>
<artifactId>demo
</artifactId>
<version>0.0.1-SNAPSHOT
</version>
<name>demo
</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-data-jdbc
</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.1.3
</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>
<exclusions>
<exclusion>
<groupId>org.junit.vintage
</groupId>
<artifactId>junit-vintage-engine
</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot
</groupId>
<artifactId>spring-boot-maven-plugin
</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins
</groupId>
<artifactId>maven-surefire-plugin
</artifactId>
<version>2.22.1
</version>
<configuration>
<skipTests>true
</skipTests>
</configuration>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources
</directory>
<includes>
<include>**/*.yml
</include>
<include>**/*.xml
</include>
</includes>
<filtering>true
</filtering>
</resource>
<resource>
<directory>src/main/java
</directory>
<includes>
<include>**/*.yml
</include>
<include>**/*.xml
</include>
</includes>
<filtering>true
</filtering>
</resource>
</resources>
</build>
</project>
resource
mybatis-config.xml
<?xml version="1.0" encoding="UTF8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/user?useSSL=true&useUnicode=true&characterEncodeinbg=UTF-8&serverTimezone=Asia/Shanghai"/>
<property name="username" value="root"/>
<property name="password" value="DanielNi!@#54$%"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/example/demo/UserMapper.xml"/>
</mappers>
</configuration>
application.yml
spring:
profiles:
active: dev
application-dev.yml
spring:
profiles:
active: dev
datasource:
url: jdbc
:mysql
://localhost
:3306/mybatis
?autoReconnect=true
&useUnicode=true
&characterEncoding=utf8
&useSSL=false
&allowMultiQueries=true
&serverTimezone=Asia/Shanghai
username: root
password: DanielNi
!@
driver-class-name: com.mysql.cj.jdbc.Driver
server:
port: 8080
mybatis:
mapper-locations: classpath
:com.example.deom/mapper/
*Mapper.xml
logging:
level:
com:
example:
mapper : debug
2. controller
package com
.example
.demo
.controller
;
import com
.example
.demo
.entity
.User
;
import com
.example
.demo
.service
.UserService
;
import org
.springframework
.beans
.factory
.annotation
.Autowired
;
import org
.springframework
.boot
.autoconfigure
.EnableAutoConfiguration
;
import org
.springframework
.stereotype
.Controller
;
import org
.springframework
.stereotype
.Service
;
import org
.springframework
.web
.bind
.annotation
.PathVariable
;
import org
.springframework
.web
.bind
.annotation
.RequestMapping
;
import org
.springframework
.web
.bind
.annotation
.RestController
;
@Controller
@RestController
@RequestMapping("/testBoot")
public class UserController {
@Autowired
private UserService userService
;
@RequestMapping("getUser/{id}")
public String
GetUser(@PathVariable int id
) {
return userService
.Sel(id
).toString();
}
}
3.entity
package com
.example
.demo
.entity
;
public class User {
private Integer id
;
private String userName
;
private String passWord
;
private String realName
;
public Integer
getId() {
return id
;
}
public void setId(Integer id
) {
this.id
= id
;
}
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 String
getRealName() {
return realName
;
}
public void setRealName(String realName
) {
this.realName
= realName
;
}
@Override
public String
toString() {
return "User{" +
"id=" + id
+
", userName='" + userName
+ '\'' +
", passWord='" + passWord
+ '\'' +
", realName='" + realName
+ '\'' +
'}';
}
}
4.mapper
package com
.example
.demo
.mapper
;
import com
.example
.demo
.entity
.User
;
import org
.apache
.ibatis
.annotations
.Select
;
import org
.springframework
.stereotype
.Repository
;
@Repository
public interface UserMapper {
User
Sel(int id
);
}
5.service
package com
.example
.demo
.service
;
import com
.example
.demo
.entity
.User
;
import com
.example
.demo
.mapper
.UserMapper
;
import org
.springframework
.beans
.factory
.annotation
.Autowired
;
import org
.springframework
.stereotype
.Service
;
@Service
public class UserService {
@Autowired
UserMapper userMapper
;
public User
Sel(int id
){
return userMapper
.Sel(id
);
}
}
6.启动入口
package com
.example
.demo
;
import org
.mybatis
.spring
.annotation
.MapperScan
;
import org
.springframework
.boot
.SpringApplication
;
import org
.springframework
.boot
.autoconfigure
.SpringBootApplication
;
@SpringBootApplication
@MapperScan("com.example.demo")
public class DemoApplication {
public static void main(String
[] args
) {
SpringApplication
.run(DemoApplication
.class, args
);
}
}
7.最后启动
最后启动,浏览器输入地址看看吧:http://localhost:8080/testBoot/getUser/1)
8.修改启动banner
在resource下新建banner.txt ,然后通过 更改banner的网址 修改banner,写入banner.txt就好。