在我剛開(kāi)始學(xué)習(xí)Springboot時(shí)渐白,我的師傅(算半個(gè))叫我先寫(xiě)一個(gè)Springboot的增刪改查,不管用什么辦法,叫我在一周內(nèi)交付代碼逞频,我就去谷歌搜纯衍,看別人博客里寫(xiě)的各種五花八門的辦法,項(xiàng)目在別人手中好好的苗胀,一到我的手上就開(kāi)始報(bào)錯(cuò)襟诸,而且這個(gè)bug改完,下一個(gè)bug就在不遠(yuǎn)處等著我解決基协,反正在解決bug的過(guò)程中也學(xué)習(xí)了很多歌亲,跑遠(yuǎn)了,說(shuō)回正題澜驮,
先建一個(gè)Springboot項(xiàng)目陷揪,目錄結(jié)構(gòu)如下
image.png
給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數(shù)據(jù)庫(kù)連接池 -->
<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> <!-- 這個(gè)需要為 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> {
/**
* 通過(guò)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);
/**
* 根據(jù) 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);
/**
* 根據(jù) id 刪除用戶信息
*/
@Delete("DELETE from user_entity WHERE id = #{id}")
void deleteUser(@Param("id") int id);
}
service層
@Service
public class UserService {
@Resource
private UserMapper userMapper;
/**
* 根據(jù)id查找用戶
*/
public List<UserEntity> selectUserByName(int id) {
return userMapper.findUserByName(id);
}
/**
* 插入兩個(gè)用戶
*/
public void insertService(int id,String username,String password,int age,String address,int type) {
userMapper.insertUser(id, username ,password, age,address,type);
}
/**
* 根據(jù)id 修改用戶
*/
public void Update(int id,String username,String password,int age,String address,int type){
userMapper.updateUser(id, username ,password, age,address,type);
}
/**
* 根據(jù)id 刪除用戶
*/
public void deleteService(int id) {
userMapper.deleteUser(id);
}
}
controller層寫(xiě)增刪改查方法
@Controller
@RequestMapping()
public class UsersController {
@Autowired
private UserService userService;
/**
* 根據(jù)id查找用戶
*/
@RequestMapping(value = "/query",method = RequestMethod.GET)
@ResponseBody
public List<UserEntity> testQuery(int id) {
return userService.selectUserByName(id);
}
/**
* 插入兩個(gè)用戶
*/
@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";
}
/**
* 根據(jù)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 "修改成功";
}
/**
* 根據(jù)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
其實(shí)很簡(jiǎn)單,自從學(xué)習(xí)過(guò)Springboot后我就不喜歡SpringMVC了杂穷,哈哈哈悍缠,我就是一個(gè)喜新厭舊的壞銀。耐量。飞蚓。