boot mybatis mysql_SpringBoot+Mybatis+MySql学习
介紹一下SpringBoot整合mybatis,數(shù)據(jù)庫選用的是mysql。
首先創(chuàng)建數(shù)據(jù)庫
CREATE DATABASE test;
建表以及插入初始數(shù)據(jù)(sql是從navicat中導出的)
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for user
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_name` varchar(255) NOT NULL,
`user_password` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=13 DEFAULT CHARSET=latin1;
-- ----------------------------
-- Records of user
-- ----------------------------
BEGIN;
INSERT INTO `user` VALUES (1, 'dalaoyang', '13');
INSERT INTO `user` VALUES (2, 'xiaoli', '123');
INSERT INTO `user` VALUES (3, 'xiaoxiongmao', '123');
COMMIT;
SET FOREIGN_KEY_CHECKS = 1;
下圖為項目目錄結(jié)構(gòu),
java---
controller包負責測試整合
dao包作為數(shù)據(jù)操作層
entity作為數(shù)據(jù)實體類
resources---
mapper寫dao層對應實現(xiàn)的sql
mybatis里面是mybatis配置,包含typeAlias等等
sql里面放的是上面寫的建表數(shù)據(jù)及sql
image
接下來直接上代碼,啟動類沒有修改,代碼如下
package com.dalaoyang;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
@SpringBootApplication
public class SpringbootMybatisApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootMybatisApplication.class, args);
}
}
application.properties包含了數(shù)據(jù)庫配置,mybatis配置,代碼如下:
##端口號
server.port=8888
##檢查 mybatis 配置是否存在,一般命名為 mybatis-config.xml
mybatis.check-config-location =true
##配置文件位置
mybatis.config-location=classpath:mybatis/mybatis-config.xml
## mapper xml 文件地址
mybatis.mapper-locations=classpath*:mapper/*Mapper.xml
##日志級別
logging.level.com.yang.dao=debug
##數(shù)據(jù)庫url
spring.datasource.url=jdbc:mysql://localhost:3306/test?characterEncoding=utf8&useSSL=false
##數(shù)據(jù)庫用戶名
spring.datasource.username=root
##數(shù)據(jù)庫密碼
spring.datasource.password=root
##數(shù)據(jù)庫驅(qū)動
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
實體類User
package com.dalaoyang.entity;
import org.apache.ibatis.type.Alias;
/**
* @author dalaoyang
* @Description
* @project springboot_learn
* @package com.dalaoyang.entity
* @email 397600342@qq.com
* @date 2018/4/5
*/
@Alias("user")
public class User {
private int id;
private String user_name;
private String user_password;
public User(String user_name, String user_password) {
this.user_name = user_name;
this.user_password = user_password;
}
public User(int id, String user_name, String user_password) {
this.id = id;
this.user_name = user_name;
this.user_password = user_password;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUser_name() {
return user_name;
}
public void setUser_name(String user_name) {
this.user_name = user_name;
}
public String getUser_password() {
return user_password;
}
public void setUser_password(String user_password) {
this.user_password = user_password;
}
}
dao層代碼
package com.dalaoyang.dao;
import com.dalaoyang.entity.User;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
/**
* @author dalaoyang
* @Description
* @project springboot_learn
* @package com.dalaoyang.dao
* @email 397600342@qq.com
* @date 2018/4/5
*/
@Mapper
public interface UserMapper {
User findUserByUsername(String username);
void updateUserByUsername(User user);
void deleteUserByUsername(String username);
void saveUser(User user);
List getUserList();
}
UserMapper.xml代碼
SELECT * FROM user
WHERE user_name=#{1}
UPDATE USER SET USER_PASSWORD=#{user_password} WHERE USER_NAME=#{user_name}
DELETE FROM USER WHERE USER_NAME=#{1}
INSERT INTO USER (user_password,user_name) VALUES (#{user_password},#{user_name})
SELECT * FROM USER
mybatis-config.xml
/p>
"http://mybatis.org/dtd/mybatis-3-config.dtd">
pom文件
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
com.dalaoyang
springboot_mybatis
0.0.1-SNAPSHOT
jar
springboot_mybatis
springboot_mybatis
org.springframework.boot
spring-boot-starter-parent
1.5.9.RELEASE
UTF-8
UTF-8
1.8
org.springframework.boot
spring-boot-starter-web
org.mybatis.spring.boot
mybatis-spring-boot-starter
1.3.1
org.springframework.boot
spring-boot-devtools
runtime
mysql
mysql-connector-java
runtime
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-maven-plugin
UserController
package com.dalaoyang.controller;
import com.dalaoyang.dao.UserMapper;
import com.dalaoyang.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* @author dalaoyang
* @Description
* @project springboot_learn
* @package com.dalaoyang.controller
* @email 397600342@qq.com
* @date 2018/4/5
*/
@RestController
public class UserController {
@Autowired
UserMapper userMapper;
//http://localhost:8888/getUser?username=xiaoli2
@RequestMapping("/getUser")
public String getUser(String username){
User user =userMapper.findUserByUsername(username);
return user!=null ? username+"的密碼是:"+user.getUser_password():"不存在用戶名為"+username+"的用戶";
}
//http://localhost:8888/updateUser?username=xiaoli2&password=123
@RequestMapping("/updateUser")
public String updateUser(String password,String username){
User user = new User(username,password);
userMapper.updateUserByUsername(user);
return "success!";
}
//http://localhost:8888/addUser?username=xiaoli2&password=123
@RequestMapping("/addUser")
public String addUser(String username,String password){
User user = new User(username,password);
userMapper.saveUser(user);
return "success!";
}
//http://localhost:8888/addUser?username=xiaoli2
@RequestMapping("/deleteUser")
public String deleteUser(String username){
userMapper.deleteUserByUsername(username);
return "success!";
}
//http://localhost:8888/getUserList
@RequestMapping("/getUserList")
public List getUserList(String username, String password){
return userMapper.getUserList();
}
}
啟動項目,訪問controller上面對應的注釋上的地址即可以測試,
其中包含了簡單的增刪改查,SpringBoot整合Mybatis就這樣完成了。
源碼下載 :大老楊碼云
總結(jié)
以上是生活随笔為你收集整理的boot mybatis mysql_SpringBoot+Mybatis+MySql学习的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: odyssey react鉴定_Nike
- 下一篇: mysql data目录 说明_mysq