Spring Mybatis整合小例子

1、新建JavaProject工程 spring-mybatis、部署spring、新建包、添加properties属性文件。目录如下:

2、applicationConetxt.xml文件已经把mybatis-config.xml中的dataSource、mappers等配置整合成bean或peroperty

<?xml version="1.0" encoding="UTF-8"?>  
<beans xmlns="http://www.springframework.org/schema/beans"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"  
    xmlns:context="http://www.springframework.org/schema/context"  
    xmlns:mvc="http://www.springframework.org/schema/mvc"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans    
    http://www.springframework.org/schema/beans/spring-beans-3.1.xsd    
    http://www.springframework.org/schema/context    
    http://www.springframework.org/schema/context/spring-context-3.1.xsd    
    http://www.springframework.org/schema/mvc    
    http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
  
  <!-- 自动扫描所有的包,加载装配所有的实体类bean -->
  <context:component-scan base-package="com"/>
 
 <!-- 加载配置文件 -->
 <context:property-placeholder location="classpath:*.properties" />
 
 <!-- 配置数据源 -->
 <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  <property name="driverClassName" value="${driver}"/>
  <property name="url" value="${url}"/>
  <property name="username" value="${user}"/>
  <property name="password" value="${password}"/>
 </bean>
 
 <!-- sqlSessionFactory -->
 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  <!-- 加载 mapper.xml的配置文件 -->
  <property name="mapperLocations" value="com/mapper/*.xml"/>
  <!-- 添加所需的数据源 -->
  <property name="dataSource" ref="dataSource"/>
 </bean>
 
 <!-- 扫描Dao包,加载接口映射类 -->
 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
 	<property name="basePackage" value="com.mapper" />
 	<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> 	
 </bean>
 
 
 <!-- (事务管理)transaction manager -->  
    <bean id="transactionManager"  
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
        <property name="dataSource" ref="dataSource" />  
    </bean> 
 
 
</beans>

3、mapper文件

 package com.mapper;
import java.util.List;
import com.model.Student;

public interface StudentMapper {

	public List<Student> getAllStudent();
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.mapper.StudentMapper">

	<select id="getAllStudent" resultType="com.model.Student">
		select * from student
	</select>

</mapper>

4、实体类Student

package com.model;

public class Student {
	private int sno;
	private String sname;
	private double score;
	
	public int getSno() {
		return sno;
	}
	public void setSno(int sno) {
		this.sno = sno;
	}
	public String getSname() {
		return sname;
	}
	public void setSname(String sname) {
		this.sname = sname;
	}
	public double getScore() {
		return score;
	}
	public void setScore(double score) {
		this.score = score;
	}
	public Student(int sno, String sname, double score) {
		super();
		this.sno = sno;
		this.sname = sname;
		this.score = score;
	}
	public Student() {
		super();
	}
	@Override
	public String toString() {
		return "Student [sno=" + sno + ", sname=" + sname + ", score=" + score + "]";
	}
	
	
}

5、从配置文件中获得ApplicationContext上下文的工具类

package com.tool;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class AppContext {

	private static ApplicationContext appContext;
	
	static{
		appContext=new ClassPathXmlApplicationContext("applicationContext.xml");
	}
	
	public static <T>T getBean(Class<T> t){
		return appContext.getBean(t);
	}
}

6、service业务包

package com.service;

import java.util.List;

import com.model.Student;

public interface InterStudentService {

	public List<Student> showStudentList();
}
package com.service;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.mapper.StudentMapper;
import com.model.Student;
import com.tool.AppContext;

@Service
public class StudentServiceImpl implements InterStudentService {

	@Resource
	private StudentMapper studentMapper;	

	@Override
	public List<Student> showStudentList() {
		
		//ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
		
		studentMapper=AppContext.getBean(StudentMapper.class);
		
		return studentMapper.getAllStudent();
	}

}

7、测试类Test

package com.test;

import com.service.InterStudentService;
import com.service.StudentServiceImpl;
import com.tool.AppContext;

public class Test {

	public static void main(String[] args) {
		
		//InterStudentService stuService=new StudentServiceImpl();
		
		InterStudentService stuService=AppContext.getBean(StudentServiceImpl.class);
		
		System.out.println(stuService.showStudentList());
		
	}
	
}

8、结果

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值