1.构建Springboot项目
利用springboot整合jdbctemplate,并不需要导入其他的依赖,具体的项目结构图如图
2.写domain层
数据库映射的实体类
package com.jkk.springboot_jdbc_template.domain;
/**
* @author jkk
*/
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import java.io.Serializable;
/**
* Users类将数据库中的users表映射在一起
* @author jkk
* @see java.io.Serializable
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class Users implements Serializable {
private String username;
private String password;
private String idNumber;
private String phoneNumber;
private String gender;
private String dataStatus;
private static final long serialVersionUID = 1L;
}
3.mapper层
是对数据库的操作,可以直接注入
package com.jkk.springboot_jdbc_template.mapper;
import com.jkk.springboot_jdbc_template.domain.Users;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import java.util.List;
/**
* 这一层是数据库访问层.
* <p>主要是对数据库的增删查改</p>
* @author jkk
*/
@Repository
public class UsersMapper {
@Autowired
JdbcTemplate jdbcTemplate;
public int addUser(Users users){
return jdbcTemplate.update("insert into users(username,password,id_number,phone_number,gender,data_status) values (?,?,?,?,?,?)",
users.getUsername(),users.getPassword(),users.getIdNumber(),users.getPhoneNumber(),users.getGender(),users.getDataStatus());
}
public int updateUser(Users users){
return jdbcTemplate.update("update users set password=?,id_number=?,phone_number=?,gender=?,data_status=? where username=?",
users.getPassword(),users.getIdNumber(),users.getPhoneNumber(),users.getGender(),users.getDataStatus(),users.getUsername());
}
public int deleteByUserName(String userName){
return jdbcTemplate.update("delete from users where username = ?",userName);
}
public Users getByUserName(String userName){
return jdbcTemplate.queryForObject("select * from users where username = ?",new BeanPropertyRowMapper<>(Users.class),userName);
}
public List<Users> getAllUser(){
return jdbcTemplate.query("select * from users",new BeanPropertyRowMapper<>(Users.class));
}
}
4.service层
package com.jkk.springboot_jdbc_template.service;
import com.jkk.springboot_jdbc_template.domain.Users;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* @author jkk
*/
public interface UserService {
/**
* 添加用户信息
* @param users
* @return
*/
public int addUser(Users users);
/**
* 修改用户信息
* @param users
* @return
*/
public int updateUser(Users users);
/**
* 删除用户信息
* @param userName
* @return
*/
public int deleteByUserName(String userName);
/**
* 查找单个信息
* @param userName
* @return
*/
public Users getByUserName(String userName);
/**
* 查询所有信息
* @return
*/
public List<Users> getAllUser();
}
package com.jkk.springboot_jdbc_template.service.impl;
import com.jkk.springboot_jdbc_template.domain.Users;
import com.jkk.springboot_jdbc_template.mapper.UsersMapper;
import com.jkk.springboot_jdbc_template.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
*具体的业务逻辑层.
* @see com.jkk.springboot_jdbc_template.service.UserService
* @author jkk
*/
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UsersMapper usersMapper;
@Override
public int addUser(Users users) {
return usersMapper.addUser(users);
}
@Override
public int updateUser(Users users) {
return usersMapper.updateUser(users);
}
@Override
public int deleteByUserName(String userName) {
return usersMapper.deleteByUserName(userName);
}
@Override
public Users getByUserName(String userName) {
return usersMapper.getByUserName(userName);
}
@Override
public List<Users> getAllUser() {
return usersMapper.getAllUser();
}
}
5.controller层
package com.jkk.springboot_jdbc_template.controller;
import com.jkk.springboot_jdbc_template.domain.Users;
import com.jkk.springboot_jdbc_template.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* @author jkk
*/
@RestController
public class UsersController {
@Autowired
private UserService userService;
@GetMapping("/user")
public String userTest(){
Users users = new Users();
users.setUsername("test4");
users.setPassword("test4");
users.setIdNumber("444");
users.setPhoneNumber("444");
users.setGender("男");
users.setDataStatus("0");
int add = userService.addUser(users);
int delete = userService.deleteByUserName("test3");
users.setPhoneNumber("555");
int update = userService.updateUser(users);
Users test4 = userService.getByUserName("test4");
System.out.println(test4);
List<Users> allUser = userService.getAllUser();
System.out.println(allUser);
// return "插入了"+add+"行,删除了"+delete+"行,更新了"+update+"行";
return test4.toString();
}
}