public int insert(User user); public int deleteById(Integer id); public int update(User user); public User getById(Integer id); }
@Repository public class UserDaoImpl implements UserDao {
@Autowired private JdbcTemplate jdbcTemplate; @Override public int insert(User user) { String sql = "insert into user(id,username,password,birthday) values(?,?,?,?)"; return this.jdbcTemplate.update( sql, user.getId(), user.getUsername(), user.getPassword(), user.getBirthday() ); }
@Override public int deleteById(Integer id) { String sql = "delete from user where id = ?"; return this.jdbcTemplate.update(sql,id); }
@Override public int update(User user) { String sql = "update user set password = ? where id = ?"; return this.jdbcTemplate.update( sql, user.getPassword(), user.getId() ); }
@Override public User getById(Integer id) { String sql = "select * from user where id = ?"; return this.jdbcTemplate.queryForObject(sql, new RowMapper<User>() {
@Override public User mapRow(ResultSet rs, int rowNum) throws SQLException { User user = new User(); user.setId(rs.getInt("id")); user.setUsername(rs.getString("username")); user.setPassword(rs.getString("password")); user.setBirthday(rs.getDate("birthday")); return user; } },id); }
@RunWith(SpringRunner.class) @SpringBootTest public class UserDaoTest {
@Autowired private UserDao userDao;
@Test public void testInsert() { User user = new User(); user.setId(1); user.setUsername("张三"); user.setPassword("zhangsan"); user.setBirthday(new Date()); int result = this.userDao.insert(user); System.out.println(result); } @Test public void testGetById() { User user = this.userDao.getById(1); System.out.println(user.getUsername()); } @Test public void testUpdate() { User user = new User(); user.setId(1); user.setPassword("zhangsan123"); this.userDao.update(user); } @Test public void testDeleteById() { int result = this.userDao.deleteById(1); System.out.println(result); } }
@RunWith(SpringRunner.class) @SpringBootTest public class RoleRepositoryTest {
@Autowired private RoleRepository roleRepository; @Test public void testInsert() { Role role = new Role(); role.setName("管理员"); role.setDescr("测试"); Role result = this.roleRepository.save(role); System.out.println(result); } @Test public void testFindOne() { Role role = this.roleRepository.findOne(1); System.out.println(role); } @Test public void testUpdate() { Role role = new Role(); role.setId(1); role.setName("管理员"); role.setDescr("控制权限"); Role result = this.roleRepository.save(role); System.out.println(result); } @Test public void testDelete() { this.roleRepository.delete(1); } }
public void insert(Department department); public Department getById(Integer id); public void update(Department department); public void deleteById(Integer id); }
<insert id="insert" parameterType="com.light.springboot.domain.Department"> insert into department(id,name,descr) values(#{id},#{name},#{descr}) </insert> <select id="getById" parameterType="java.lang.Integer" resultType="com.light.springboot.domain.Department"> select id,name,descr from department where id = #{id} </select> <update id="update" parameterType="com.light.springboot.domain.Department"> update department set descr = #{descr} where id = #{id} </update> <delete id="deleteById" parameterType="java.lang.Integer"> delete from department where id = #{id} </delete> </mapper>
@RunWith(SpringRunner.class) @SpringBootTest public class DepartmentTest {
@Autowired private DepartmentMapper departmentMapper; @Test public void testInsert() { Department department = new Department(); department.setId(1); department.setName("研发部"); department.setDescr("开发产品"); this.departmentMapper.insert(department); } @Test public void testGetById() { Department department = this.departmentMapper.getById(1); System.out.println(department); } @Test public void testUpdate() { Department department = new Department(); department.setId(1); department.setDescr("开发高级产品"); this.departmentMapper.update(department); } @Test public void testDeleteById() { this.departmentMapper.deleteById(1); } }