MyBatis与Spring整合的两种方式

1、第一步:导入jar包

spring的jar包、Mybatis的jar包、Spring+mybatis的整合包、Mysql的数据库驱动jar包、数据库连接池的jar包

MyBatis与Spring整合的两种方式_第1张图片

2、加入配置文件:

Spring的配置文件——applicationContext.xml

这里有一点需要注意:在配置sqlSessionFactory工厂时,在注入数据库连接池的时候property的属性名是dataSource,不要乱写(很容易写成dataSourse),否则会报如下错误:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactoryBean' defined in class path resource [applicationContext.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'dataSourse'ofbean class [org.mybatis.spring.SqlSessionFactoryBean]: Bean property 'dataSourse' is not writable or has an invalid setter method. Did you mean'dataSource'?



	
	
	
    
    
    	
		
		
		
		
		
    
    
    
        
    	
    	
    	
    

	
	
	  	
	
	
	
		
		
		
		
	

MyBatis的配置文件——sqlMapConfig.xml




	
	
		
		
	
	
	
		
	
	

链接数据库的配置信息文件——db.property

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8
jdbc.username=root
jdbc.password=123456

3、Dao开发:有两种形式。一种是传统的DAO开发,另一种是Mapper动态代理开发

(1)传统Dao开发

pojo类:

package com.qianjin.pojo;

import java.util.Date;

public class User{
	private Integer id;
	private String username;// 用户姓名
	private String sex;// 性别
	private Date birthday;// 生日
	private String address;// 地址
	
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public Date getBirthday() {
		return birthday;
	}
	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	@Override
	public String toString() {
		return "User [id=" + id + ", username=" + username + ", sex=" + sex + ", birthday=" + birthday + ", address="
				+ address + "]";
	}

}
package com.qianjin.dao;

import com.qianjin.pojo.User;

public interface UserDao {
	User queryUserById(Integer id);
}
package com.qianjin.dao;

import org.mybatis.spring.support.SqlSessionDaoSupport;

import com.qianjin.pojo.User;

public class UserDaoImpl extends SqlSessionDaoSupport implements UserDao{

	@Override
	public User queryUserById(Integer id) {
		User user = this.getSqlSession().selectOne("queryUserById", 10);
		return user;
	}
	

}

测试运行:

package com.qianjin.dao.test;

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

import com.qianjin.dao.UserDao;
import com.qianjin.pojo.User;
public class UserDaoTest {
	ApplicationContext context;
	@Before
    public void init(){
    	 this.context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
    }
	@Test
	public void testQueryUserById(){
		UserDao userDao=this.context.getBean(UserDao.class);
		User user = userDao.queryUserById(10);
		System.out.println(user);
		
	}
}

(2)Mapper动态代理开发(UserMapper接口和UserMapper.xml配置文件要在同一个包下)

配置UserMapper接口:

package sqlmapper;

import com.qianjin.pojo.User;

public interface UserMapper {
	User queryUserById(Integer id);
}

配置UserMapper.xml配置文件:




	
	

在Spring的配置文件applicationContext.xml中配置UserMapper的配置信息(上面的代码中已经配置,此处再贴一遍)


	
		
		
		
		
	

写测试类进行测试:

package com.qianjin;

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

import com.qianjin.pojo.User;

import sqlmapper.UserMapper;

public class JunitTest {
	private ApplicationContext context;
	@Before
	public void init() {
		//通过类路径配置spring配置文件的位置信息
		this.context=new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
	}
	@Test
	public void testQueryUserById() {
		//根据spring配置文件获取bean
		UserMapper userMapper = (UserMapper) this.context.getBean("userMapper");
		User user = userMapper.queryUserById(10);
		System.out.println(user);
	}
}

 

你可能感兴趣的:(java,Spring,MyBatis)