启动类
//@MapperScan("")
//@SpringApplication
package com.example.demo;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@MapperScan("com.example.demo.mapper")
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
userMapper.java
//@Repository
package com.example.demo.mapper;
import com.example.demo.entity.User;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface UserMapper {
//1.通过id查询用户信息
User getUser(int id);
//2.通过id删除用户信息
int delete(int id);
//3.更改用户信息
int update(User user);
//4.插入用户信息
int save(User user);
//5.查询所有用户信息
List<User> selectAll();
}
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="com.example.demo.mapper.UserMapper">
<resultMap id="BaseResultMap" type="com.example.demo.entity.User">
<result column="id" jdbcType="INTEGER" property="id" />
<result column="name" jdbcType="VARCHAR" property="name" />
<result column="salary" jdbcType="DOUBLE" property="salary" />
</resultMap>
<!--查询用户信息-->
<select id="getUser" resultType="com.example.demo.entity.User">
select * from water where id = #{id}
</select>
<!--删除用户信息-->
<delete id="delete" parameterType="int">
delete from water where id=#{id}
</delete>
<!--返回所有用户信息-->
<select id="selectAll" resultType="com.example.demo.entity.User">
select * from water
</select>
<!--增加用户信息-->
<insert id="save" parameterType="com.example.demo.entity.User" >
insert into water
<trim prefix="(" suffix=")" suffixOverrides="," >
<if test="id != null" >
id,
</if>
<if test="name != null" >
name,
</if>
<if test="salary != null" >
salary,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides="," >
<if test="id != null" >
#{id,jdbcType=INTEGER},
</if>
<if test="name != null" >
#{name,jdbcType=VARCHAR},
</if>
<if test="salary != null" >
#{salary,jdbcType=DOUBLE},
</if>
</trim>
</insert>
<!--根据id更改用户信息-->
<update id="update" parameterType="com.example.demo.entity.User">
update water
<set >
<if test="name != null" >
name = #{name,jdbcType=VARCHAR},
</if>
<if test="salary != null" >
salary = #{salary,jdbcType=DOUBLE},
</if>
</set>
where id = #{id,jdbcType=INTEGER}
</update>
</mapper>
userService.java
//@Service
//@Autowired
package com.example.demo.service;
import com.example.demo.entity.User;
import com.example.demo.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserService {
@Autowired
private UserMapper userMapper;
public User getUser(int id){
return userMapper.getUser(id);
}
public int delete(int id){
return userMapper.delete(id);
}
public int update(User user){
return userMapper.update(user);
}
public int save(User user){
return userMapper.save(user);
}
public List<User> selectAll(){
return userMapper.selectAll();
}
}
userController.java
//@RestController
//@RequestMapping
//@Autowired
package com.example.demo.controller;
import com.example.demo.entity.User;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.xml.ws.Service;
import java.util.List;
@RestController
@RequestMapping("/seven")
public class UserController {
@Autowired
private UserService userService;
//通过id得到用户信息
@RequestMapping(value = "/getUser/{id}", method = RequestMethod.GET)
public String getUser(@PathVariable int id){
return userService.getUser(id).toString();
}
//通过id删除用户信息
@RequestMapping(value = "/delete", method = RequestMethod.GET)
public String delete(int id){
int result = userService.delete(id);
if(result >= 1){
return "删除成功!";
}else{
return "删除失败!";
}
}
//更改用户信息
@RequestMapping(value = "/update", method = RequestMethod.GET)
public String update(User user){
int result = userService.update(user);
if(result >= 1){
return "更新成功!";
}else{
return "更新失败!";
}
}
//插入用户信息
@RequestMapping(value = "/insert", method = RequestMethod.GET)
public int insert(User user){
return userService.save(user);
}
//查询所有用户的信息
@RequestMapping(value = "/selectAll")
@ResponseBody //理解为:单独作为响应体,这里不调用实体类的toString方法
public List<User> listUser(){
return userService.selectAll();
}
}
application.yml
spring:
profiles:
active: dev
application-dev.xml
#服务器端口配置
server:
port: 8081
#数据库配置
spring:
datasource:
username: root
password: root(密码)
url: jdbc:mysql://localhost:3306/aa?useUnicode=true&characterEncoding=utf-8&nullCatalogMeansCurrent=true&useSSL=true&&serverTimezone=Asia/Shanghai
driver-class-name: com.mysql.cj.jdbc.Driver
#mybatis配置
mybatis:
mapper-locations: classpath:mapping/*.xml
type-aliases-package: com.example.demo.entity
#showSQL
logging:
level:
com.example.demo.entity: debug
标签:return,SpringBoot,int,改查,id,增删,import,com,public
From: https://www.cnblogs.com/liuzijin/p/17399956.html