一、向数据库插入数据 (新增)
1.1、在UserDAO中添加addUser方法
1.2、在UserMapp.xml中匹配addUser方法,并维护sql语句
<insert id="addUser">
insert into t_user (id,user_name,passwd) values (#{id},#{name},#{passwd})
</insert>
1.3、在controller中测试
@RequestMapping("/t8")
public int t8(){
int count = dao.addUser(new User(1001L,"mike","321"));
return count;
}
测试结果
二、从数据库查询数据(登录)
2.1、在UserDAO中添加login方法
2.2、在UserMapp.xml中匹配login方法,并维护sql语句
<select id="login" resultType="demo.entity.User">
select * from t_user where user_name=#{userName} and passwd=#{passwd}
</select>
2.3、在controller中测试
@RequestMapping("/login")//登录
public String login(String userName,String passwd){
User u = dao.login(userName,passwd);
if(u != null){
return "登录成功";
}else{
return "登录失败";
}
}
测试结果
三、根据id修改数据(改)
3.1、在UserDAO中添加updateUser方法
3.2、在UserMapp.xml中匹配updateUser方\法,并维护sql语句
<update id="updateUser">
update t_user set user_name=#{name},passwd=#{passwd} where id=#{id}
</update>
3.3、在controller中测试
@RequestMapping("/t9")
public int t9(){
int count = dao.updateUser(new User(1001L,"kangkang","7788"));
return count;
}
测试结果
四、根据id删除数据(删)
4.1、在UserDAO中添加deleteUser方法
4.2、在UserMapp.xml中匹配deleteUser方法,并维护sql语句
<delete id="deleteUser">
delete from t_user where id=#{id}
</delete>
4.3、在controller中测试
@RequestMapping("/t10")
public int t10(){
int count = dao.deleteUser(1001L);
return count;
}
测试结果
源代码合集
User源代码
import lombok.Data;
import lombok.NoArgsConstructor;
@Data//set和get还有toString
@AllArgsConstructor//有参构造
@NoArgsConstructor//无参构造
@TableName("t_user")//设置表名
public class User {
@TableId
private Long id;
@TableField("user_name")//修改name和数据库对应
private String name;
private String passwd;
}
UserDAO源代码
package demo.dao;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import demo.entity.User;
public interface UserDAO extends BaseMapper<User> {
/**
* 登录
*/
public User login(String userName,String passwd);
/**
* 新增数据
*/
public int addUser(User user);
/**
* 根据id修改数据
*/
public int updateUser(User user);
/**
* 根据id删除数据
*/
public int deleteUser(Long id);
}
TestConeroller源代码
package demo.controller;
import demo.entity.User;
import demo.dao.UserDAO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Arrays;
import java.util.List;
@RestController
@Slf4j
public class TestController {
@Autowired(required = false)
private UserDAO dao;
@RequestMapping("/t2")//增
public int insertUser(){
int count = dao.insert(new User(null,"admin","123"));
return count;
}
@RequestMapping("/t3")//查
public List<User> t3(){
List<User> users= dao.selectList(null);
return users;
}
@RequestMapping("/t4")//根据id查
public User t4(){
//根据id查询
User u =dao.selectById(1001L);
return u;
}
@RequestMapping("/t5")//根据id修改数据
public int t5(){
//根据id修改
int count = dao.updateById(new User(88121345L,"mike","111"));
return count;
}
@RequestMapping("/t6")//根据id删
public int t6(){
int count = dao.deleteById(1001L);
return count;
}
@RequestMapping("/t7")//根据多个id批量删数据
public int t7(){
int count = dao.deleteBatchIds(Arrays.asList(1592420385389924353L,1592420384727224322L,1592420367253753857L));
return count;
}
@RequestMapping("/login")//登录
public String login(String userName,String passwd){
User u = dao.login(userName,passwd);
if(u != null){
return "登录成功";
}else{
return "登录失败";
}
}
@RequestMapping("/t8")
public int t8(){
int count = dao.addUser(new User(1001L,"mike","321"));
return count;
}
@RequestMapping("/t9")
public int t9(){
int count = dao.updateUser(new User(1001L,"kangkang","7788"));
return count;
}
@RequestMapping("/t10")
public int t10(){
int count = dao.deleteUser(1001L);
return count;
}
}
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="demo.dao.UserDAO">
<select id="login" resultType="demo.entity.User">
select * from t_user where user_name=#{userName} and passwd=#{passwd}
</select>
<insert id="addUser">
insert into t_user (id,user_name,passwd) values (#{id},#{name},#{passwd})
</insert>
<update id="updateUser">
update t_user set user_name=#{name},passwd=#{passwd} where id=#{id}
</update>
<delete id="deleteUser">
delete from t_user where id=#{id}
</delete>
</mapper>
application.yml源代码
server:
port: 8888
spring:
# 配置数据源信息
datasource:
# 配置数据源类型
type: com.zaxxer.hikari.HikariDataSource
# 配置连接数据库信息
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/car_manager?characterEncoding=utf-8&useSSL=false
username: root
password: 123456
#mybatisPlus相关配置
mybatis-plus:
mapper-locations: classpath:mappers/*.xml
Demo源代码
package demo;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("demo.dao")//扫描dao的路径
public class Demo {
public static void main(String[] args) {
SpringApplication.run(Demo.class,args);
}
}