栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringBoot第 7 讲:SpringBoot+MyBatis

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringBoot第 7 讲:SpringBoot+MyBatis

开发环境:SpringBoot1.5.2、MyBatis3.5、jdk1.8、MySQL5.7.20

一、创建Maven项目

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

 二、修改pom.xml

	4.0.0
	
	
		org.springframework.boot
		spring-boot-starter-parent
		1.5.2.RELEASE
	
	

	org.personal.qin.demos
	mybatis_demo
	1.0.0-SNAPSHOT
	jar

	
		UTF-8
		UTF-8
		
		1.8
	

	
		
		
			org.springframework.boot
			spring-boot-starter-web
		
		

		
		
			org.springframework
			spring-webmvc
		
		
		
		
			mysql
			mysql-connector-java
		
		

		
		
			org.mybatis.spring.boot
			mybatis-spring-boot-starter
			1.3.2
		
		
			org.mybatis
			mybatis-spring
			1.3.1
		
		
			org.mybatis
			mybatis
			3.5.0
		
		
		
		
			com.jolbox
			bonecp-spring
			0.8.0.RELEASE
		
		
	

	
		${project.artifactId}
		
			
			
				org.apache.maven.plugins
				maven-resources-plugin
				
					UTF-8
				
			
			
			
				org.apache.maven.plugins
				maven-compiler-plugin
				
					1.8
					1.8
					UTF-8
				
			
			
			
				org.springframework.boot
				spring-boot-maven-plugin
			
			
		
	
三、配置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-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 selectAll();
	
	public User selectByAuth(Map params);
	
	public int insertUser(User user);
	
	public int deleteUserById(int id);
	
	public int updateUser(User user);
}
4.2、UserMapper.xml

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




	
		select * from t_user where name=#{name} and password=#{password}
	
	
	
		insert into t_user values(default, #{name}, #{password})
	
	
	
		delete from t_user where id = #{id}
	
	
	
		update t_user set name=#{name}, password=#{password} where id=#{id}
	
五、启动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 test1(){
		return mapper.selectAll();
	}
	
	@GetMapping("/{name}/{password}")
	public User test2(@PathVariable String name, @PathVariable String password) {
		Map params = new HashMap();
		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

   

七、源代码

https://download.csdn.net/download/qzc70919700/30526342​​​​​​​

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/309715.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号