九、Spring Boot spring MVC及Mybatis整合入门(数据插入)
(一)、创建Spring Boot 项目
(二)、添加依赖(pom.xml)
1.相关依赖
<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>
<!-- spring-boot启动器-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- thymeleaf视图层 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!-- Mybatis启动器 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.3</version>
</dependency>
<!-- mysql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- 数据库连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.22</version>
</dependency>
</dependencies>
2.其它配置
<build>
<!-- 资源文件配置 -->
<resources>
<resource>
<directory>src/main/java</directory>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.*</include>
</includes>
</resource>
</resources>
<!-- 相关插件配置 -->
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
(三)、项目相关配置
# 数据库驱动
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# 数据库连接地址
spring.datasource.url=jdbc:mysql://localhost:3306/ssm?serverTimezone=UTC
# 数据库用户名
spring.datasource.username=root
# 数据库密码
spring.datasource.password=ikaros01
# 数据库连接池
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
# 实体类别名
mybatis.type-aliases-package=com.ikaros.pojo
(四)、整合MVC及Mybatis
总览:
1.实体类创建
public class Users {
public Users() {
}
private Integer id;
private String name;
private Integer age;
@Override
public String toString() {
return "Users{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
'}';
}
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 Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
2.mapper创建
@Component(value ="userMapper")
public interface UsersMapper {
void insertUsers(Users user);
}
<?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.ikaros.mapper.UsersMapper">
<insert id="insertUsers" parameterType="users">
insert into users(name, age) values(#{name}, #{age})
</insert>
</mapper>
- namespace,为对应类所在命名空间;
- id,为对应方法名称;
- parameterType,参数类型。
3.service创建
public interface UsersService {
void addUser(Users user);
}
@Service
@Transactional
public class UserServiceImpl implements UsersService {
@Autowired
private UsersMapper usersMapper;
@Override
public void addUser(Users user) {
System.out.println("mapper:" + usersMapper);
this.usersMapper.insertUsers(user);
}
}
- @Autowired,自动装配对象。不要再使用
new
去创建对象。 - @Autowired,注意mapper需要添加注解
@Component
; - @Autowired,可用
@Resource
或@Autowired(required=false)
替换。详情参考
4.controller创建
@Controller
@RequestMapping("/user")
public class UsersController {
@Autowired
private UsersService usersService;
/**
* 页面跳转,可使用文件名直接访问页面,不需要添加.html后缀
* @param page
* @return
*/
@GetMapping("/{page}")
public String showPage(@PathVariable String page){
return page;
}
/**
* 添加用户
* @param users
* @return
*/
@PostMapping("/addUser")
public String addUsers(Users users){
System.out.println("用户:" + users+ "添加成功!");
this.usersService.addUser(users);
return "ok";
}
}
- @RequestMapping(/xx),改control下所有请求前面添加/xx;
- @Autowired,自动装配对象。不要再使用
new
去创建对象。 - @Autowired,注意mapper需要添加注解
@Component
; - @Autowired,可用
@Resource
或@Autowired(required=false)
替换。详情参考
5.前端页面创建
提交表单:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>input</title>
</head>
<body>
<form th:action="@{/user/addUser}" method="post">
用户名:<input type="text" name="name"><br>
年龄:<input type=“text” name="age"><br>
<input type="submit" value="确定">
</form>
</body>
</html>
操作成功页面:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>ok</title>
</head>
<body>
成功.
</body>
</html>
6.启动类修改
添加注解@MapperScan
,扫描mapper
@SpringBootApplication
@MapperScan("com.ikaros.mapper")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
-@MapperScan,扫描mapper,包名一定要填写正确。
(五)、注意事项
- pom.xml一定要添加资源文件,否则mapper xml会出现找不到;
- 当使用注解
@Autowired
自动装配对象时,mapper类需要添加注解@Component
或注解@Autowired
修改为@Autowired(required=false)
; - service层和controller使用注解
@Autowired
自动装配对象,不要使用new
关键字创建对象; - mapper层编写时xml文件中
namespace
和id
一定要填写正确,需要仔细核实。namespace
为对应类的命名空间;id
为对应的方法名称。