Getting started with springboot and mybatis

Use many very important annotations in this project

annotation effect
@Mapper @Mapper is added to the interface class, and the corresponding interface implementation class will be generated after compilation
@MapperScan Specify the package where the interface to become the implementation class is located, and then all the interfaces under the package will generate the corresponding implementation class after compilation
@Transactional Is declarative transaction management, used on the implementation class

1. Import related packages

<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.1.RELEASE</version>
	</parent>
	<groupId>cn.liuhao</groupId>
	<artifactId>springboot-mybatis-01</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</artifactId>
		</dependency>
		<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>
		<!-- jdbc -->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
		</dependency>
		<!-- 数据库连接池 -->
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>druid</artifactId>
			<version>1.2.1</version>
		</dependency>
	</dependencies>
</project>

2. Add mybatis configuration in the configuration file

Insert picture description here

The content of the configuration file is as follows

#数据连接参数
spring.datasource.driveClassName=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/ spide01?useSSL=false&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=123456
#数据库连接池
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
#指定mybatis要起别名实体类所在的包
mybatis.type-aliases-package=cn.liuhao.spide_haotanku.pojo
#指定mapper映射文件的文件夹路径
mybatis.mapperLocations=classpath:mapper/*.xml

3. Simple project realization

3.1 Add database

-- 创建数据库
create database ssm;
-- 使用ssm
use ssm;
-- 创建表
create table users
(
	id int not null auto_increment,
	name varchar(50) default null,
	age int(11) default null,
	constraint pk_user primary key(id)
);

insert into users values (default,'张三',18)

3.2 Writing entity classes

User

package cn.liuhao.web.pojo;

public class User {
    
    

	private int id;
	private String name;
	private int age;

	public int getId() {
    
    
		return id;
	}

	public void setId(int id) {
    
    
		this.id = id;
	}

	public String getName() {
    
    
		return name;
	}

	public void setName(String name) {
    
    
		this.name = name;
	}

	public int getAge() {
    
    
		return age;
	}

	public void setAge(int age) {
    
    
		this.age = age;
	}

	public User(int id, String name, int age) {
    
    
		super();
		this.id = id;
		this.name = name;
		this.age = age;
	}

	public User(String name, int age) {
    
    
		this.name = name;
		this.age = age;
	}

}

3.3 Realize the mybatis interface binding technology (write mapper and mapped xml)

userMapper

package cn.liuhao.web.mapper;

import java.util.List;

import cn.liuhao.web.pojo.User;

public interface UserMapper {
    
    

	public int insert(User u);

	public List<User> selectAll();
}

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="cn.liuhao.web.mapper.UserMapper">
	
	<insert id="insert" parameterType="user">
		insert into users values (default,#{name},#{age})
	</insert>
	
	<select id="selectAll" resultType="user">
		select * from users
	</select>
</mapper>

3.4 Writing the service business layer

interface

package cn.liuhao.web.service;

import java.util.List;

import cn.liuhao.web.pojo.User;

public interface UserService {
    
    

	public List<User> getUsers();

	public int addUser(User u);
}

implement

package cn.liuhao.web.service.impl;

import java.util.List;

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

import cn.liuhao.web.mapper.UserMapper;
import cn.liuhao.web.pojo.User;
import cn.liuhao.web.service.UserService;

@Service
@Transactional
public class UserServiceImpl implements UserService {
    
    

	@Autowired
	UserMapper userMapper;

	@Override
	public List<User> getUsers() {
    
    

		return userMapper.selectAll();
	}

	@Override
	public int addUser(User u) {
    
    

		return userMapper.insert(u);
	}

}

3.5 controller

package cn.liuhao.web.controller;

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

import cn.liuhao.web.pojo.User;
import cn.liuhao.web.service.UserService;

@Controller
@RequestMapping("user")
public class UserController {
    
    

	@Autowired
	UserService userService;

	@RequestMapping("{page}")
	public String page(@PathVariable("page") String page) {
    
    

		return page;
	}

	@RequestMapping("showUser")
	@ResponseBody
	public Object showUsers() {
    
    

		return userService.getUsers();
	}

	@RequestMapping("addUser")
	public String addUser(String uname, String uage) {
    
    
		userService.addUser(new User(uname, Integer.parseInt(uage)));
		return "ok";
	}
}

3.6 View template

register.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<form th:action="@{/user/addUser}" method="post">
		<input type="text" name="uname"  />
		<input type="text" name="uage" />
		<input type="submit" value="添加" />
	</form>
</body>
</html>

ok.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<h2>添加成功</h2>
</body>
</html>

3.7 Startup class

package cn.liuhao;

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

/**
 * 
 * mapperScan 用来指定扫描mybatis映射文件
 * 
 * @author admin
 *
 */
@SpringBootApplication
@MapperScan("cn.liuhao.web.mapper")
public class App {
    
    

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

Guess you like

Origin blog.csdn.net/qq_42418169/article/details/109048365