Spring 和 Mybatis整合

最近闲来无事应朋友只需,整理了一下以前学习Spring框架和 Mybatis 框架的一些资料,自己写了一个小的单表增删查改的 dome.
在写之前需要需要下载好Spring的jar包和 Mybatis的 jar包,在 Mysql 建一个 tb_user 的单表.
下面是工程的一个包结构:

Spring 和 Mybatis整合_第1张图片
文件结构.png

db.properties文件内容
##oracle 和 mysql 你使用其中一个即可,根据自己的账号密码填写
##oracle
##url=jdbc:oracle:thin:@localhost:1521:orcl
##user=scott
##pwd=12345
##driver=oracle.jdbc.OracleDriver

##mysql
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/test
user=root
pwd=root

实体类 User类

public class User {
      
private Integer id;
private String name;
private String password;
private int age;
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 getPassword() {
    return password;
}
public void setPassword(String password) {
    this.password = password;
}
public int getAge() {
    return age;
}
public void setAge(int age) {
    this.age = age;
}

}

Usermapper接口类

public interface UserMapper {
/**
 * 新增数据
 *@param user
 */
int insertUser(User user);
/**
 * 更新数据
 *@param user
 */
int updateUser(User user);
 /**
 *  删除数据
 *@param user
 */
int deleteUser(User user);
/**
 * 根据name 查询
 *@param name
 */
List getUserByName(String name);

}

业务层 service 的 UserService 接口

public interface UserService {
 /**
 *注册
 *@param name
 */
public void register(User user); 
 /**
 * 通过姓名查询用户
 *@param name
 */
public List getUserByName(String name);
 /**
 * 修改用户
 *@param name
 */
void modifyUser(User user);
 /**
 * 根据id用户
 *@param name
 */
void dropUser(Integer id);

}

UserServiceImpl是 UserService 的实现类

@Service // spring容器管理对象 Spring 的注解开发
public class UserServiceImpl implements UserService {

@Autowired
private UserMapper userMapper;

@Override
public void register(User user) {
    userMapper.insertUser(user);
}

@Override
public List getUserByName(String name) {
    return this.userMapper.getUserByName(name);
}

@Override
public void modifyUser(User user) {
    this.userMapper.updateUser(user);
}

@Override
public void dropUser(Integer id) {
    User user = new User();
    user.setId(id);
    this.userMapper.deleteUser(user);
}
}

applicationContext-base.xml






    




    
        
            classpath:cn/sxt/config/commons/db.properties
        
    




    
    
    
    



applicationContext-mybatis.xml






    




    
    
        
            
            classpath:cn/sxt/mapper/*.xml
        
    
    
        cn.sxt.entity
    




    
    






applicationContext-service.xml

 




    

    


applicationContext-tx.xml

  
  



    




    




    
    
        
        
        
        
        
        
        
    




    



UserMapper.xml

  
  





    insert into tb_user (id, name, password, age)
        values(seq_user.nextVal, #{name}, #{password}, #{age})




    update tb_user
        
        
            
            name = #{name},
            
            
            password = #{password},
            
            
            age = #{age},
            
        
        
            
            and id = #{id}
            
        

 

    delete from tb_user
        where id = #{id}





接下来我们做测试,建一个测试类,到如一个 JUnit jar包
也可以用 main 做测试也行,测试就不详细说了.看代码.

public class TestSpringMyBatis {

@Test
public void testUpdate(){
    ApplicationContext context = 
            new ClassPathXmlApplicationContext("classpath:cn/lx/configurations/spring/applicationContext-*.xml");
    
    UserService userService = context.getBean(UserService.class);
    
    User user = new User();
    user.setId(3);
    user.setName("老宋");
    
    userService.modifyUser(user);
}

@Test
public void testInsert(){
    ApplicationContext context = 
            new ClassPathXmlApplicationContext("classpath:cn/lx/configurations/spring/applicationContext-*.xml");
    
    UserService userService = context.getBean(UserService.class);
    
    User user = new User();
    user.setName("天王");
    user.setPassword("xoxo");
    user.setAge(18);
    
    userService.register(user);
}

@Test
public void testService(){
    ApplicationContext context = 
            new ClassPathXmlApplicationContext("classpath:cn/lx/configurations/spring/applicationContext-*.xml");
    
    UserService userService = context.getBean(UserService.class);
    
    System.out.println(userService.getClass().getName());
    
    List users = userService.getUserByName("老王");
    for(User u : users){
        System.out.println(u.getId() + " ; " + u.getName());
    }
}

你可能感兴趣的:(Spring 和 Mybatis整合)