Spring004

第一步:编写UserMapper接口 和UserMapper.xml文件

package com.zll.dao;

import com.zll.pojo.User;
import org.apache.ibatis.annotations.Param;

/**
 * @auther zll
 * @create 2020/7/20-16:01
 */
public interface UserMapper {
    //根据用户id修改信息
    public int updateUser(@Param("id") int id, @Param("u") User user);

    //根据id删除用户
    public int deleteUser(@Param("id")int id);

    //根据id、修改密码
    public int updatePwdById(@Param("id") int id,@Param("password")String pwd);
}




    
        update smbms_user set userName=#{u.userName} where id=#{id}
    

    
        delete from smbms_user where id=#{id}
    

    
        update smbms_user set userPassword=#{u.userPassword} where id=#{id}
    

 第二步,编写接口的实现类UserMapperImpl

import org.springframework.stereotype.Repository;

/**
 * @auther zll
 * @create 2020/7/20-16:02
 */

public class UserMapperImpl implements UserMapper {

    private SqlSessionTemplate sqlSessionTemplate;

    public SqlSessionTemplate getSqlSessionTemplate() {
        return sqlSessionTemplate;
    }

    public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {
        this.sqlSessionTemplate = sqlSessionTemplate;
    }

    @Override
    public int updateUser(int id, User user) {
        return sqlSessionTemplate.getMapper(UserMapper.class).updateUser(id,user);
    }

    @Override
    public int deleteUser(int id) {
        return sqlSessionTemplate.getMapper(UserMapper.class).deleteUser(id);
    }

    @Override
    public int updatePwdById(int id, String pwd) {
        return sqlSessionTemplate.getMapper(UserMapper.class).updatePwdById(id,pwd);
    }
}

 第三步,编写UserService接口和其实现类UserServiceImpl

import com.zll.pojo.User;

/**
 * @auther zll
 * @create 2020/7/20-10:22
 */
public interface UserService {

    public boolean updateUser(int id, User user);

    public boolean delete(int id);

    public boolean updatePassword(int id, String pwd);
}

 

public class UserServiceImpl implements UserService {


    private UserMapper userMapper;

    public UserMapper getUserMapper() {
        return userMapper;
    }

    public void setUserMapper(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    @Override
    public boolean updateUser(int id, User user) {
        return userMapper.updateUser(id,user)>0;
    }

    @Override
    public boolean delete(int id) {
        return userMapper.deleteUser(id)>0;
    }

    @Override
    public boolean updatePassword(int id, String pwd) {
        return userMapper.updatePwdById(id,pwd)>0;
    }
}

 第四步:编写Spring的配置文件



    
        
        
        
        
    

    
        
        
        
        
        
        
        
        
    

    
        
        
    






    
        
        
        
    





    
        
    

 第五步:测试

@Test
    public void test1(){
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

        UserService userService = context.getBean("userService", UserService.class);

        User user = new User();
        user.setUserName("章子怡");
        userService.updateUser(20,user);
    }

 

你可能感兴趣的:(作业)