Spring-boot integrates mybatis + spring MVC

Spring-boot integrates mybatis + spring MVC
Let me give you an example of adding data to the background.
1. New maven project, import dependencies

<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>1.5.10.RELEASE</version>
	</parent>
	<groupId>cn.zds</groupId>
	<artifactId>08-spring-boot-view-jsp</artifactId>
	<version>0.0.1-SNAPSHOT</version>

	<!-- jdk1.7 Appoint thymeleaf Edition -->
	<properties>
		<java.version>1.7</java.version>
		<thymeleaf.version>3.0.2.RELEASE</thymeleaf.version>
		<thymeleaf-layout-dialect.version>2.0.4</thymeleaf-layout-dialect.version>
	</properties>

	<dependencies>
		<!-- springBoot Starter -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
	<!-- web starter -->
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-thymeleaf</artifactId>
	</dependency>

	<!-- Mybatis starter -->
	<dependency>
		<groupId>org.mybatis.spring.boot</groupId>
		<artifactId>mybatis-spring-boot-starter</artifactId>
		<version>1.1.1</version>
	</dependency>

	<!-- mysql Database Driver -->
	<dependency>
		<groupId>mysql</groupId>
		<artifactId>mysql-connector-java</artifactId>
	</dependency>

	<!-- druid Database connection pool -->
	<dependency>
		<groupId>com.alibaba</groupId>
		<artifactId>druid</artifactId>
		<version>1.0.9</version>
	</dependency>
</dependencies>

2. design table

CREATE TABLE `users` ( 
`id` int(11) NOT NULL AUTO_INCREMENT, 
`name` varchar(255) DEFAULT NULL, 
`age` int(11) DEFAULT NULL, 
PRIMARY KEY (`id`) 
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

3. Add the global configuration file application.proterties

spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/ssm?useUnicode=true&characterEncoding=utf-8
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource 
mybatis.type-aliases-package=cn.zds.pojo

mybatis.type-aliases-package is the tagged entity class (let mapper.xml not write the full name of the entity class)
4. Write entity class Users

package cn.zds.pojo;

public class Users {
	private Integer id;
	private String name;
	private Integer age;
	public Integer getId() {
		return id;
	}
	@Override
	public String toString() {
		return "Users [id=" + id + ", name=" + name + ", age=" + age + "]";
	}
	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;
	}
}

5. Write UserMapper interface binding file with UserMapper.xml interface

package cn.zds.mapper;

import cn.zds.pojo.Users;

public interface UserMapper {
	public void insertUser(Users users);
}
<?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="cn.zds.mapper.UserMapper"> 

	<insert id="insertUser" parameterType="users"> 
	insert into users(name,age) values(#{name},#{age}) 
	</insert> 

</mapper>

6. New service layer file
6.1.UserService interface

package cn.zds.service;

import cn.zds.pojo.Users;

public interface UserService {
	public void addUser(Users users);
}

6.2 UserService Impl Implementation Class

package cn.zds.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.zds.mapper.UserMapper;
import cn.zds.pojo.Users;
import cn.zds.service.UserService;

@Service
@Transactional
public class UserServiceImpl implements UserService {
	
	
	@Autowired
	private UserMapper userMapper;
	
	
	@Override
	public void addUser(Users users) {
		userMapper.insertUser(users);
		
	}
	
}

7. New Control Layer

package cn.zds.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.zds.pojo.Users;
import cn.zds.service.UserService;

@Controller
@RequestMapping("/users")
public class UserController {
	@Autowired
	private UserService userService;
	
	@RequestMapping("/{page}")
	public String showUser(@PathVariable String page){
		
		return page;
	}
	
	@RequestMapping("/addUser")
	public String addUser(Users users){
		userService.addUser(users);
		return "ok";
	}
}

Note: The first Request Mapping function is to allow browsers to enter static HTML names and access HTML files.
8. New View Layer File
add.html

<html>
<head>
<meta charset="UTF-8">
<title>demo</title>
</head>
<body>
	<form th:action="@{/users/addUser}" method="post"> 
	//User name: <input type= "text" name= "name" /><br/>
	 //User Age: <input type="text" name="age"/><br/> 
	<input type="submit" value="Determine"/><br/> </form>
</body>
</html>

ok.html

<html>
<head>
<meta charset="UTF-8">
<title>demo</title>
</head>
<body>
	ok
</body>
</html>

9. Write starter

package cn.zds;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * SpringBoot Startup class
 *
 *
 */
@SpringBootApplication
@MapperScan("cn.zds.mapper")
public class App {

	public static void main(String[] args) {
		SpringApplication.run(App.class, args);
	}
}

Description: @MapperScan("cn.zds.mapper")
It's for the project to scan mapper files
10. Project structure

11. Problem you may encounter: You can't find a database-driven solution: Check whether there are spaces behind each application.proterties file configuration

Tags: Spring Mybatis Thymeleaf Maven

Posted on Mon, 07 Oct 2019 15:24:24 -0700 by 44justin