pojo类:
package org.example.entity;
public class MyUser
{
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;
}
@Override
public String toString() {
return "MyUser{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
'}';
}
}
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="org.example.mapper.UserMapper">
<select id="findAll" resultType="org.example.entity.MyUser">
select * from users
</select>
<insert id="saveUser" parameterType="org.example.entity.MyUser">
insert into users(id,name,age) values(#{id},#{name},#{age})
</insert>
<insert id="saveUser2" parameterType="org.example.entity.MyUser">
insert into users(id,name,age) values(#{id},#{name},#{age})
</insert>
<insert id="saveInsertUser" parameterType="org.example.entity.MyUser">
<selectKey keyProperty="id" keyColumn="id" order="AFTER" resultType="int">
SELECT LAST_INSERT_ID();
</selectKey>
insert into users(id,name,age) values(#{id},#{name},#{age})
</insert>
<update id="updateUser" parameterType="org.example.entity.MyUser">
update users set name = #{name},age = #{age} where id = #{id}
</update>
<delete id="deleteUser" parameterType="java.lang.Integer">
delete from users where id = #{id}
</delete>
<delete id="deleteUserByName" parameterType="java.lang.String">
delete from users where name = #{name}
</delete>
<select id="findById" parameterType="java.lang.Integer" resultType="org.example.entity.MyUser">
select * from USERs where id = #{id}
</select>
</mapper>
mapper接口:
package org.example.mapper;
import org.example.entity.MyUser;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface UserMapper
{
public List<MyUser> findAll();
void saveUser(MyUser user);
int saveUser2(MyUser user);
int saveInsertUser(MyUser user);
int updateUser(MyUser user);
int deleteUser(Integer id);
int deleteUserByName(String name);
MyUser findById(Integer id);
}
service接口:
package org.example.service;
import org.example.entity.MyUser;
import java.util.List;
public interface UserService
{
List<MyUser> findAll();
void saveUser(MyUser user);
int saveUser2(MyUser user);
int saveInsertUser(MyUser user);
int updateUser(MyUser user);
int deleteUser(Integer id);
int deleteUserByName(String name);
MyUser findById(Integer id);
}
实现service接口:
package org.example.service.Impl;
import org.example.entity.MyUser;
import org.example.mapper.UserMapper;
import org.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserServiceImpl implements UserService
{
@Autowired
UserMapper userMapper;
@Override
public List<MyUser> findAll()
{
return userMapper.findAll();
}
@Override
public void saveUser(MyUser user)
{
userMapper.saveUser(user);
}
public int saveUser2(MyUser user)
{
return userMapper.saveUser2(user);
}
public int saveInsertUser(MyUser user)
{
return userMapper.saveInsertUser(user);
}
public int updateUser(MyUser user)
{
return userMapper.updateUser(user);
}
public int deleteUser(Integer id)
{
return userMapper.deleteUser(id);
}
public int deleteUserByName(String name)
{
return userMapper.deleteUserByName(name);
}
public MyUser findById(Integer id)
{
return userMapper.findById(id);
}
}
控制器:
package org.example.controller;
import java.util.List;
import org.example.entity.MyUser;
import org.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class MyUserController
{
@Autowired
private UserService userService;
@GetMapping(value = "/1")
public List<MyUser> home1()
{
// 查询所有网站
List<MyUser> users = userService.findAll();
for (MyUser user : users)
{
System.out.println(user.getId()+" "+user.getName()+" "+user.getAge());
}
return users;
}
@GetMapping(value = "/2")
public void home2()
{
MyUser user2 = new MyUser();
user2.setName("TTT");
user2.setAge(7878);
userService.saveUser(user2);
}
@GetMapping(value = "/3")
public int home3()
{
MyUser user2 = new MyUser();
user2.setName("RRR");
user2.setAge(6666);
int a = userService.saveUser2(user2);
System.out.println(a);
return a;
}
@GetMapping(value = "/4")
public int home4()
{
MyUser user2 = new MyUser();
user2.setName("ccc");
user2.setAge(321123);
userService.saveInsertUser(user2);
int a = user2.getId();
System.out.println(a);
System.out.println(user2);
return a;
}
@GetMapping(value = "/5")
public int updateUser()
{
MyUser user2 = new MyUser();
user2.setId(1013);
user2.setName("sprint-boot-1013");
user2.setAge(199999);
int a = userService.updateUser(user2);
System.out.println(a);
return a;
}
@GetMapping(value = "/6")
public int deleteUser()
{
int a = userService.deleteUser(1012);
System.out.println(a);
return a;
}
@GetMapping(value = "/7")
public int deleteUserByName()
{
int a = userService.deleteUserByName("sprint-boot-1013");
System.out.println(a);
return a;
}
@GetMapping(value = "/8")
public void findById()
{
MyUser user001 = userService.findById(1011);
System.out.println(user001);
}
}
查询: