mybatis和spring整合

pom.xml




  4.0.0

  cn.kgc
  spring_mybatis
  1.0-SNAPSHOT

  spring_mybatis
  
  http://www.example.com

  
    UTF-8
    1.7
    1.7
  

  
    
      junit
      junit
      4.11
      test
    
    
    
      mysql
      mysql-connector-java
      6.0.6
    
    
    
      org.projectlombok
      lombok
      1.18.2
    
    
    
      org.mybatis
      mybatis
      3.4.4
    
    
    
      org.mybatis
      mybatis-spring
      2.0.1
    
    
      org.springframework
      spring-jdbc
      5.1.7.RELEASE
    
    
      org.springframework
      spring-tx
      5.1.7.RELEASE
    
    

    
      log4j
      log4j
      1.2.17
    
    
      commons-logging
      commons-logging
      1.2
    
    
    
      org.springframework
      spring-beans
      5.1.7.RELEASE
    
    
      org.springframework
      spring-context
      5.1.7.RELEASE
    
    
      org.springframework
      spring-core
      5.1.7.RELEASE
    

    
      org.springframework
      spring-webmvc
      5.1.7.RELEASE
    
    
    
      org.springframework
      spring-aop
      5.1.7.RELEASE
    
    
      org.aspectj
      aspectjweaver
      1.9.1
    
    
      aopalliance
      aopalliance
      1.0
    
    
    
      org.apache.commons
      commons-dbcp2
      2.6.0
    
    
    
      com.zaxxer
      HikariCP
      3.3.1
    
    
      org.slf4j
      slf4j-api
      1.7.25
    
    
      org.slf4j
      slf4j-nop
      1.7.26
    
    
    
      com.alibaba
      druid
      1.1.17
    
  

  
    
      
        
        
          maven-clean-plugin
          3.1.0
        
        
        
          maven-resources-plugin
          3.0.2
        
        
          maven-compiler-plugin
          3.8.0
        
        
          maven-surefire-plugin
          2.22.1
        
        
          maven-jar-plugin
          3.0.2
        
        
          maven-install-plugin
          2.5.2
        
        
          maven-deploy-plugin
          2.8.2
        
        
        
          maven-site-plugin
          3.7.1
        
        
          maven-project-info-reports-plugin
          3.0.0
        
      
    
  


dao
usermapper.java

//dao的注解
@Repository("userMapper")
public interface UserMapper {
    //    查询
    List getUserList(User user);
    //    增加
    boolean addUser(User user);
}

user.java

public class User {
    private Integer id;
    private String userCode;
    private String userName;
    private String userPassword;
    private Integer gender;
    private Date birthday;
    private String phone;
    private String address;
    private Integer userRole;
    private Integer createBy;//创建者
    private Date createDate;
    private Integer modifyBy;//更新者
    private Date modifyDate;//更新时间
    private String userRoleName;//用户角色名称

    set,get等代码省略
}

service
接口

public interface UserService {
    //查询
    ListfindUsersWithConditions(User user);
    //添加一个用户
    boolean addUser(User user);
    //添加多个用户
    void addUsers(List users);
}

实现类

  1. @Transactional(propagation = Propagation.REQUIRED)
    为类或方法添加事务处理(回滚还是不不回滚)
  2. @Service(“userService”)
    用于标注业务类
  3. @Autowired
    实现Bean 的装配,默认按类型装配
@Transactional(propagation = Propagation.REQUIRED)
@Service("userService")
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Override
    //查询
    public List findUsersWithConditions(User user) {
        return userMapper.getUserList(user);
    }
    @Override
    //添加
    public boolean addUser(User user) {
        return userMapper.addUser(user);
    }
    @Override
    //添加多个用户   事务
    public void addUsers(List users) {
        for(User user:users){
            addUser(user);
            throw new RuntimeException("text error");
        }
    }
}

UserMapper.xml





    
        
    
    
    
    
    
        INSERT INTO smbms_user(userCode,userName,userPassword)VALUES(#{userCode},#{userName},#{userPassword})
    


配置文件拆分

applicationContext-dao.xml





	
	
	
	
		
	


applicationContext-service.xml




	
	
	


applicationContext.xml



         //导入dao和service的配置文件
	
	

	
	
		
	

/////
数据源

	
		
				  
		
		
		
		
	
	 
	
	
	
	
	
	
	
	
	
	
/////

	
	
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		

		
		
		
		
		
		
	

	
	
		
		
		
		
		
	


	
	
		
	
	
	

	/////
	
	
		
			
			
		
	
	
	
		
		
	
	//////
	

myBatis-config.xml




    
        
    

测试类

public class AppTest {
    @Test
    public void shouldAnswerWithTrue()
    {
        assertTrue( true );
    }
    @Test
    //查询
    public void getUser(){
        ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
        UserService userService=(UserService)context.getBean("userService");
        User user=new User();
        user.setUserName("赵");
        user.setUserRole(3);

        List userList=new ArrayList<>();
        userList = userService.findUsersWithConditions(user);

        for(User userResult:userList){
            System.out.println("姓名:"+userResult.getUserName()+"  "+"角色:"+userResult.getUserRole());
        }
    }
    @Test
    //添加
    public void addUser(){
        ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext-mybatis.xml");
        UserService userService=(UserService)context.getBean("userService");

        User user=new User();
        user.setUserName("李家少爷");
        user.setUserRole(3);

        boolean b = userService.addUser(user);
        System.out.println("添加=="+b);
    }
    @Test
    //添加多名用户  事务
    public void addUsers(){
        ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext-mybatis.xml");
        UserService userService=(UserService)context.getBean("userService");

        User user=new User();
        user.setUserName("李家少爷");
        user.setUserRole(3);
        List users =new ArrayList<>();
        users.add(user);
        users.add(user);
        userService.addUsers(users);
    }
}

你可能感兴趣的:(Spring框架)