mybatis学习四:mybatis之SqlSessionTemplate的使用

1、引入相关依赖:


    org.springframework
    spring-webmvc
    4.3.18.RELEASE


    org.springframework
    spring-jdbc
    4.3.21.RELEASE


    org.mybatis
    mybatis
    3.4.6


    org.mybatis
    mybatis-spring
    1.3.2

2、编写spring配置文件:

需要添加组件扫描,并且注入org.mybatis.spring.SqlSessionTemplate



 
    
    
    
    
    
    
        
        
        
        
        
        
    
    
    
        
        
        
        
        
        
    
    
    
        
        
    

3、编写mybatis配置文件:




    
        
    
    
        
    

4、编写Entity、Mapper映射接口、Mapper映射接口实现类、Mapper映射文件:

package org.spring.teach.entity;
 
public class User {
 
    private Integer id;
    private String name;
    private String username;
    private String password;
    private String gender;
 
    public Integer getId() {
        return id;
    }
 
    public void setId(Integer id) {
        this.id = id;
    }
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
 
    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 getGender() {
        return gender;
    }
 
    public void setGender(String gender) {
        this.gender = gender;
    }
 
    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", gender='" + gender + '\'' +
                '}';
    }
}
package org.spring.teach.dao;
 
import org.spring.teach.annotations.MyBatisRepository;
import org.spring.teach.entity.User;
 
import java.util.List;
 
public interface UserMapper {
 
    List findAll();
 
    User findById(Integer id);
 
    int save(User user);
 
    int delete(Integer id);
 
    int update(User user);
}
@Repository("userMapper")
public class UserMapperImpl implements UserMapper {

    /*
     * 不用考虑提交事务和关闭SqlSession,
     * SqlSessionTemplate已经处理了。
     */
    @Autowired
    @Qualifier("sst")
    private SqlSessionTemplate sst;

    @Override
    public List findAll() {
        return sst.selectList("org.spring.teach.dao.UserMapper.findAll");
    }

    @Override
    public User findById(Integer id) {
        return sst.selectOne("org.spring.teach.dao.UserMapper.findById", id);
    }

    @Override
    public int save(User user) {
        return sst.insert("org.spring.teach.dao.UserMapper.save", user);
    }

    @Override
    public int delete(Integer id) {
        return sst.delete("org.spring.teach.dao.UserMapper.delete", id);
    }

    @Override
    public int update(User user) {
        return sst.update("org.spring.teach.dao.UserMapper.update", user);
    }
}




    
		insert into t_user values(t_user_seq.nextval,#{username},#{name},#{password},#{gender})
	
 
    
 
    
    
        update t_user set username = #{username},name = #{name},password = #{password},gender = #{gender} where id = #{id}
    
    
        delete from t_user where id = #{id1}
    

5、测试:

public class TestCase {

    private AbstractApplicationContext app;
    private UserMapper dao;

    @Before
    public void before() {
        app = new ClassPathXmlApplicationContext("spring-mybatis.xml");
        //这个bean的名称需要和UserMapperImpl里面@Repository注解的一致
        dao = app.getBean("userMapper", UserMapper.class);
        System.out.println("........." + dao);
    }

    @After
    public void after() {
        app.close();
    }

    @Test
    public void save() {
        User user = new User();
        user.setUsername("赵六");
        user.setName("赵六");
        user.setPassword("534634634");
        user.setGender("男");
        int row = dao.save(user);
        System.out.println(row);
    }

    @Test
    public void findAll() {
        List list = dao.findAll();
        System.out.println(list);
    }

    @Test
    public void findById() {
        User user = dao.findById(1);
        System.out.println(user);
    }

    @Test
    public void update() {
        User user = new User();
        user.setId(62);
        user.setUsername("赵六");
        user.setName("赵六");
        user.setPassword("gfnvcvsdv");
        user.setGender("女");
        int row = dao.update(user);
        System.out.println(row);
    }

    @Test
    public void delete() {
        int row = dao.delete(64);
        System.out.println(row);
    }

}

mybatis学习四:mybatis之SqlSessionTemplate的使用_第1张图片

你可能感兴趣的:(Mybatis,spring)