SpringBoot第 7 讲:SpringBoot+MyBatis

99 阅读1分钟

开发环境:SpringBoot1.5.2、MyBatis3.5、jdk1.8、MySQL5.7.20 一、创建Maven项目

参考:SpringBoot第 1 讲:HelloWorld_秦毅翔的专栏-CSDN博客

二、修改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>
	<!-- SpringBoot支持01、parent:Begin -->
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.2.RELEASE</version>
	</parent>
	<!-- SpringBoot支持01、parent:End -->
 
	<groupId>org.personal.qin.demos</groupId>
	<artifactId>mybatis_demo</artifactId>
	<version>1.0.0-SNAPSHOT</version>
	<packaging>jar</packaging>
 
	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<!-- java版本 -->
		<java.version>1.8</java.version>
	</properties>
 
	<dependencies>
		<!-- SpringBoot:Begin -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<!-- SpringBoot:End -->
 
		<!-- SpringMVC:Begin -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
		</dependency>
		<!-- SpringMVC:End -->
		<!-- MySQL:Begin -->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
		</dependency>
		<!-- MySQL:End -->
 
		<!-- mybatis:Begin -->
		<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
			<version>1.3.2</version>
		</dependency>
		<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis-spring</artifactId>
			<version>1.3.1</version>
		</dependency>
		<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis</artifactId>
			<version>3.5.0</version>
		</dependency>
		<!-- mybatis:End -->
		<!-- BoneCP连接池(JDBC):Begin -->
		<dependency>
			<groupId>com.jolbox</groupId>
			<artifactId>bonecp-spring</artifactId>
			<version>0.8.0.RELEASE</version>
		</dependency>
		<!-- BoneCP连接池(JDBC):End -->
	</dependencies>
 
	<build>
		<finalName>${project.artifactId}</finalName>
		<plugins>
			<!-- 资源文件拷贝插件 -->
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-resources-plugin</artifactId>
				<configuration>
					<encoding>UTF-8</encoding>
				</configuration>
			</plugin>
			<!-- java编译插件 -->
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<configuration>
					<source>1.8</source>
					<target>1.8</target>
					<encoding>UTF-8</encoding>
				</configuration>
			</plugin>
			<!-- SpringBoot支持03、添加SpringBoot的插件支持:Begin -->
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
			<!-- SpringBoot支持03、添加SpringBoot的插件支持:End -->
		</plugins>
	</build>
</project>

三、配置MyBatis 3.1、jdbc.properties

#jdbc.driver=com.mysql.cj.jdbc.Driver #5.8
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://10.211.55.4:3306/t_mybatis?characterEncoding=utf-8&useSSL=false
jdbc.username=root
jdbc.password=Aa123123.
jdbc.period_in_minutes=60
jdbc.max_age_in_minutes=30
jdbc.max_connections_per_partition=100
jdbc.min_connections_per_partition=5

3.2、po和mapper路径配置Cast.java

package demo.mybatis.config;
 
public class Cast {
	/****************配置MyBatis*****************/
	//配置mybatis-config.xml文件的路径(配置MyBatis特有的增强功能)
	public static final String MYBATIS_CONFIG = "classpath:mybatis-config.xml";
	//配置PO实体的包路径
	public static final String PO_PACKAGE = "demo.mybatis.entity";
	//配置DAO(Mapper)接口的包路径
	public static final String MAPPER_PACKAGE = "demo.mybatis.mapper";
	//配置DAO(Mapper)xml文件的扫描路径
	public static final String MAPPER_SCANNER = "classpath:mappers/*.xml";
 
}

四、自定义Mapper 4.1、UserMapper接口

package demo.mybatis.mapper;
 
import java.util.List;
import java.util.Map;
 
import demo.mybatis.entity.User;
 
public interface UserMapper {
 
	public List<User> selectAll();
	
	public User selectByAuth(Map<String, Object> params);
	
	public int insertUser(User user);
	
	public int deleteUserById(int id);
	
	public int updateUser(User user);
}

4.2、UserMapper.xml

前提是要在Cast正确配置po和mapper接口的包路径

<?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="demo.mybatis.mapper.UserMapper">
	<select id="selectAll" resultType="User">
		select * from t_user
	</select>
	
	<select id="selectByAuth" parameterType="Map" resultType="User">
		select * from t_user where name=#{name} and password=#{password}
	</select>
	
	<insert id="insertUser" parameterType="User">
		insert into t_user values(default, #{name}, #{password})
	</insert>
	
	<delete id="deleteUserById" parameterType="int">
		delete from t_user where id = #{id}
	</delete>
	
	<update id="updateUser" parameterType="User">
		update t_user set name=#{name}, password=#{password} where id=#{id}
	</update>
</mapper>

五、启动BootApplication

package demo.mybatis;
 
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
import demo.mybatis.entity.User;
import demo.mybatis.mapper.UserMapper;
 
@SpringBootApplication
@RestController
@RequestMapping("user")
public class MyBatisBootApplication {
	
	@Autowired
	private UserMapper mapper;
	
	@GetMapping
	public List<User> test1(){
		return mapper.selectAll();
	}
	
	@GetMapping("/{name}/{password}")
	public User test2(@PathVariable String name, @PathVariable String password) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("name", name);
		params.put("password", password);
		return mapper.selectByAuth(params);
	}
	
	@PostMapping("/{name}/{password}")
	public String test3(@PathVariable String name, @PathVariable String password) {
		if(mapper.insertUser(new User(0, name, password)) > 0) {
			return "添加用户成功";
		} else {
			return "添加用户失败";
		}
	}
	
	@DeleteMapping("/{id}")
	public String test3(@PathVariable int id) {
		if(mapper.deleteUserById(id) > 0) {
			return "删除用户成功";
		} else {
			return "删除用户失败";
		}
	}
	
	@PutMapping("/{id}/{name}/{password}")
	public String test4(@PathVariable int id, @PathVariable String name, @PathVariable String password) {
		if(mapper.updateUser(new User(id, name, password)) > 0) {
			return "修改用户成功";
		} else {
			return "修改用户失败";
		}
	}
 
	public static void main(String[] args) {
		SpringApplication.run(MyBatisBootApplication.class, args);
	}
}

六、测试

数据库

浏览器请求:

http://127.0.0.1:8080/user