在我刚开始学习Springboot时,我的师傅(算半个)叫我先写一个Springboot的增删改查,不管用什么办法,叫我在一周内交付代码,我就去谷歌搜,看别人博客里写的各种五花八门的办法,项目在别人手中好好的,一到我的手上就开始报错,而且这个bug改完,下一个bug就在不远处等着我解决,反正在解决bug的过程中也学习了很多,跑远了,说回正题,
先建一个Springboot项目,目录结构如下
给pom.xml文件添加的依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.0.7.1</version>
</dependency>
<!-- alibaba的druid数据库连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.9</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 热部署模块 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional> <!-- 这个需要为 true 热部署才有效 -->
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-core</artifactId>
<version>1.2.4</version>
</dependency>
</dependencies>
entity层
public class UserEntity {
private int id;
private int type;
private String username;
private String password;
private String address;
private int age;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
}
mapper层
@Mapper
public interface UserMapper extends BaseMapper<UserEntity> {
/**
* 通过id查询用户信息
*/
@Select("SELECT * FROM user_entity WHERE id = #{id}")
List<UserEntity> findUserByName(@Param("id") int id);
/**
* 插入用户信息
*/
@Insert("INSERT INTO user_entity(id, username,password,age,address,type) VALUES(#{id}, #{username},#{password},#{age}, #{address}, #{type})")
void insertUser(@Param("id") int id, @Param("username") String username,@Param("password") String password,@Param("age") int age, @Param("address") String address,@Param("type") int type);
/**
* 根据 id 更新用户信息
*/
@Update("UPDATE user_entity SET id = #{id},username = #{username},password = #{password},age=#{age},address= #{address},type=#{type} WHERE id = #{id}")
void updateUser(@Param("id") int id, @Param("username") String username,@Param("password") String password,@Param("age") int age, @Param("address") String address,@Param("type") int type);
/**
* 根据 id 删除用户信息
*/
@Delete("DELETE from user_entity WHERE id = #{id}")
void deleteUser(@Param("id") int id);
}
service层
@Service
public class UserService {
@Resource
private UserMapper userMapper;
/**
* 根据id查找用户
*/
public List<UserEntity> selectUserByName(int id) {
return userMapper.findUserByName(id);
}
/**
* 插入两个用户
*/
public void insertService(int id,String username,String password,int age,String address,int type) {
userMapper.insertUser(id, username ,password, age,address,type);
}
/**
* 根据id 修改用户
*/
public void Update(int id,String username,String password,int age,String address,int type){
userMapper.updateUser(id, username ,password, age,address,type);
}
/**
* 根据id 删除用户
*/
public void deleteService(int id) {
userMapper.deleteUser(id);
}
}
controller层写增删改查方法
@Controller
@RequestMapping()
public class UsersController {
@Autowired
private UserService userService;
/**
* 根据id查找用户
*/
@RequestMapping(value = "/query",method = RequestMethod.GET)
@ResponseBody
public List<UserEntity> testQuery(int id) {
return userService.selectUserByName(id);
}
/**
* 插入两个用户
*/
@RequestMapping(value ="/insert",method = RequestMethod.GET)
public String testInsert(int id,String username,String password,int age,String address,int type) {
userService.insertService(id, username ,password, age,address,type);
//return userService.selectAllUser();
return "user/delete";
}
/**
* 根据id 修改用户
*/
@RequestMapping(value = "/update", method = RequestMethod.GET)
@ResponseBody
public String update(int id,String username,String password,int age,String address,int type) {
userService.Update(id, username , password,age,address,type);
return "修改成功";
}
/**
* 根据id 删除用户
*/
@RequestMapping(value = "/delete",method = RequestMethod.GET)
public String testDelete(int id) {
userService.deleteService(id);
return "user/delete";
}
}
自定义属性配置
application.properties文件
server.port=8088
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/springboot_test?useUnicode=true&characterEncoding=utf8&characterSetResults=utf8&useSSL=false
spring.datasource.username=root
spring.datasource.password=root
其实很简单,自从学习过Springboot后我就不喜欢SpringMVC了,哈哈哈,我就是一个喜新厌旧的坏银。。。