Spring整合MyBatis

重点在applicationContext.xml

配置pom.xml依赖


        
        
            org.springframework
            spring-context
            5.1.8.RELEASE
        
        
            org.springframework
            spring-aspects
            5.1.8.RELEASE
        
        
            org.springframework
            spring-tx
            5.1.8.RELEASE
        
        
            org.springframework
            spring-jdbc
            5.1.8.RELEASE
        
        
        
            mysql
            mysql-connector-java
            5.1.47
        
        
        
            com.alibaba
            druid
            1.1.0
        
        
        
            org.mybatis
            mybatis
            3.5.3
        
        
            org.mybatis
            mybatis-spring
            2.0.3
        
        
        
            org.slf4j
            slf4j-log4j12
            1.7.26
        
        
        
            org.springframework
            spring-test
            5.1.8.RELEASE
        
        
            junit
            junit
            4.12
        
    
    
        
            
            
                src/main/java
                
                    **/*.xml
                
            
            
                src/main/resources
            
        
    

配置log4j.properties配置

log4j.rootLogger=DEBUG,A1

log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=[%t] [%c]-[%p] %m%n

配置数据源db.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3305/spring?characterEncoding=UTF-8
jdbc.username=root
jdbc.password=

 配置applicationContext.xml




    


    

    
        
        
        
        
    

    
        
    

    
    
        
        
        
    

创建表

CREATE TABLE `t_user` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) DEFAULT NULL,
  `money` float DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=14 DEFAULT CHARSET=utf8;

创建pojo包User类

public class User {
    private Integer id;
    private String name;
    private Float money;

    public User(String name, Float money) {
        this.name = name;
        this.money = money;
    }

    public User() {
    }

    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 Float getMoney() {
        return money;
    }

    public void setMoney(Float money) {
        this.money = money;
    }
}

mapper 包UserMapper接口

public interface UserMapper {

    public void addUser(User user);
}

 mapper 包UserMapper.xml映射文件




    
		insert into t_user(name,money) values(#{name},#{money})
	

service包UserServiceImpl类

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

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

测试类junit下的Client 

//加载Spring类
@RunWith(SpringJUnit4ClassRunner.class)
//配置文件
@ContextConfiguration("classpath:applicationContext.xml")
public class Client {
    @Autowired
    private UserService userService;
    @Test
    public void testAdd(){
        userService.addUser(new User("张wu丰",4000F));

        userService.addUser(new User("宋远桥",2000F));
    }
}

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