Spring-Mybatis整合

1.在创建好的工程中导入架包

(1)spring需要的包

IOC架包

commons-logging-1.1.1.jar
spring-beans-4.1.3.RELEASE.jar
spring-context-4.1.3.RELEASE.jar
spring-core-4.1.3.RELEASE.jar
spring-expression-4.1.3.RELEASE.jar

AOP架包

aspectjweaver-1.6.11.jar
spring-aop-4.1.3.RELEASE.jar
spring-aspects-4.1.3.RELEASE.jar 

mybatis架包

mybatis-3.2.7.jar 

Spring整合mybatis需要的jar包

commons-dbcp-1.2.2.jar
commons-pool-1.3.jar
mybatis-spring-1.2.2.jar
spring-tx-4.1.3.RELEASE.jar 

其他的架包

log4j-1.2.17.jar
log4j-api-2.0-rc1.jar
log4j-core-2.0-rc1.jar
mysql-connector-java-8.0.11.jar

及其依赖包 

(2) 编写配置文件

mybatis配置文件






	
		
	
	

	
		
	

Spring配置文件




	
	
	
	
		
		
		
		
		
		
	
	
	
	
		
		
		
		
	

	
	
		
		
	
	
	

jdbc.properties

jdbc.dirverClassName=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mydb?useSSL=false&serverTimezone=GMT
jdbc.username=root
jdbc.password=root

(3)实体类

package com.zpark.bean;

import java.io.Serializable;
import java.util.Date;

public class User implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private int id;
	private String usernmae;
	private	Date birthday;
	private String sex;
	private String address;
	private int password;
	
	
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getUsernmae() {
		return usernmae;
	}
	public void setUsernmae(String usernmae) {
		this.usernmae = usernmae;
	}
	public Date getBirthday() {
		return birthday;
	}
	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	public int getPassword() {
		return password;
	}
	public void setPassword(int password) {
		this.password = password;
	}
	@Override
	public String toString() {
		return "User [id=" + id + ", usernmae=" + usernmae + ", birthday=" + birthday + ", sex=" + sex + ", address="
				+ address + ", password=" + password + "]";
	}
	
	
	
	
}

(4)编写接口使用mybatis动态代理

UserMapper.java

package com.zpark.mapper;

import com.zpark.bean.User;

public interface UserMapper {

	public User findUserById(Integer id);
}

UserMapper.xml




	

(5)测试

package com.zpark.test;

import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.zpark.bean.User;
import com.zpark.mapper.UserMapper;
import com.zpark.mapper.UserMapper;

public class TestSM {

	private ApplicationContext applicationContext;
	@Before
	public void setUp() throws Exception{
		//初始化spring运行环境
		applicationContext = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
	}
	@Test
	public void testFindUserById() throws Exception {
		UserMapper userMapper = applicationContext.getBean(UserMapper.class);
		User user = userMapper.findUserById(1);
		System.out.println(user);
		
	}
}

(6)测试结果

log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
User [id=1, usernmae=null, birthday=null, sex=2, address=null, password=1]

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