1.配置POM.xml文件 ,配置项目所需jar
2.配置WEB.xml:
    配置spring监听器

    
        contextConfigLocation
        classpath:application-context.xml
    
    
    
        org.springframework.web.context.ContextLoaderListener
    

3.创建anotation.xml配置文件
    
    


        
    
    
    

4. 创建jdbc.properties和jdbc.xml,并配置:
 

   driverClass=com.mysql.jdbc.Driver
    jdbcUrl=jdbc:mysql://localhost:3306/shop?characterEncoding=UTF-8
    user=root
    password=
    
    
    
        
        
        
        
    

5.配置property.xml文件,读取JDBC配置
 

   
        
            
                
                classpath:properties/jdbc.properties
            
        
     

6.配置mybatis.xml文件

    
    
        
        
        
    
    
    
    
        
    

7.事务管理transation.xml:    

    
    
        
    
    
    
    

8.UserInfoDAO.xml

  


    
    
        insert into user_info (user_name,user_sex)
        values(#{userName},#{userSex})
    
    

9.事务管理注释:@Transactional



import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.liu.croe.bean.UserInfo;
import cn.liu.croe.dao.UserInfoDAO;
import cn.liu.croe.service.IUserInfoService;

@Service
@Transactional
public class UserInfoServiceImpl implements IUserInfoService {

    @Autowired
    private UserInfoDAO userdao;
    
    public void addUser(UserInfo user) {
        
        int i = userdao.add(user);
        System.out.println(i);
        
        throw new RuntimeException("运行时异常");
        
    }

}


10.测试类:

package cn.shop.userinfo;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4Cla***unner;
import cn.liu.croe.bean.UserInfo;
import cn.liu.croe.service.IUserInfoService;

@RunWith(SpringJUnit4Cla***unner.class)
@ContextConfiguration(locations = "classpath:application-context.xml")
public class UserTest {
    @Autowired
    private IUserInfoService service;
    @Test
    public void testAdd() {
        UserInfo user = new UserInfo();
        user.setUserName("秋香");
        user.setUserSex("女");
        service.addUser(user);
    }
}