SM整合-方式二

package org.lanqiao.entity;

public class Student {
	private int stuNo;
	private String stuName;
	private int stuAge;
	public int getStuNo() {
		return stuNo;
	}
	public void setStuNo(int stuNo) {
		this.stuNo = stuNo;
	}
	public String getStuName() {
		return stuName;
	}
	public void setStuName(String stuName) {
		this.stuName = stuName;
	}
	public int getStuAge() {
		return stuAge;
	}
	public void setStuAge(int stuAge) {
		this.stuAge = stuAge;
	}
	
}
<?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:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
    					http://www.springframework.org/schema/beans/spring-beans.xsd
    					http://www.springframework.org/schema/mvc
    					http://www.springframework.org/schema/mvc/spring-mvc.xsd
    					http://www.springframework.org/schema/context
    					http://www.springframework.org/schema/context/spring-context.xsd">
	
	<bean id="config" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
		<property name="locations">
			<array>
				<value>classpath:db.properties</value>
			</array>
		</property>
	</bean>
	
	<!--  第一种方式生成mapper 对象
	<bean id="studentMapper" class="org.lanqiao.dao.impl.StudentDaoImpl">
	 将spring配置的sqlSessionFactory 交给dao层 
		<property name="sqlSessionFactory" ref ="sqlSessionFactory"></property>
	</bean>
	-->
	<!-- 第二种方式生成mapper  对象
	<bean id="studentMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
		<property name="mapperInterface" value="org.lanqiao.mapper.StudentMapper"></property>
		<property name="sqlSessionFactory" ref ="sqlSessionFactory"></property>
	</bean>
	 -->
	<!-- 第三 种方式生成mapper 对象(批量产生多个mapper)
		批量产生Mapper对象在SpringIOC中的id值默认就是首字母小写接口名(首字母小写接口名=id
	 --> 
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="sqlSessionFactoryBeanName" value ="sqlSessionFactory"/>
		<!-- 指定批量产生哪个包的mapper对象 -->
		<property name="basePackage" value="org.lanqiao.mapper"></property> 
	</bean>
	
	<bean id="studentService" class="org.lanqiao.service.impl.StudentServiceImpl">
		<property name="studentMapper" ref="studentMapper"></property>
	</bean>
	
	<!-- 配置数据源 -->
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
		<property name="driverClassName" value="${driver}"></property>
		<property name="url" value="${url}"></property>
		<property name="username" value="${username}"></property>
		<property name="password" value="${password}"></property> 
	</bean>
	<!-- 在SpringIoc容器中创建 MyBatis的核心类 SqlSessionFactory 
	不需要自己写,别人已经写好了
	-->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
	<!-- 访问数据库要有数据源 -->
		<property name="dataSource" ref="dataSource"></property>
		<!-- 加载mapper.xml路径 -->
		<property name="mapperLocations" value="org/lanqiao/mapper/*.xml"></property>
	</bean>
	
</beans>
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/ssm
username=root
password=lmt568899
maxActive=500
maxIdle=1000
package org.lanqiao.mapper;

import org.lanqiao.entity.Student;

public interface StudentMapper {
	public void addStudent(Student student);
}
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"

"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- namespace:该mapper.xml是映射文件唯一标识 -->
<mapper namespace="org.lanqiao.mapper.StudentMapper">
	<select id="queryStudentByStuno" parameterType="int"
		resultType="org.lanqiao.entity.Student">
		select * from student where stuNo= #{stuNo}
	</select>
	<insert id="addStudent" parameterType="org.lanqiao.entity.Student">
		insert into student(stuNo,stuName,stuAge)
		values(#{stuNo},#{stuName},#{stuAge})
	</insert>
</mapper>
package org.lanqiao.dao.impl;

import org.apache.ibatis.session.SqlSession;
import org.lanqiao.entity.Student;
import org.lanqiao.mapper.StudentMapper;
import org.mybatis.spring.support.SqlSessionDaoSupport;

public class StudentDaoImpl extends SqlSessionDaoSupport implements StudentMapper{
	@Override
	public void addStudent(Student student){
		SqlSession session = super.getSqlSession();
		// 和 conf.xml一样
		StudentMapper stuDao = session.getMapper(StudentMapper.class);
		stuDao.addStudent(student);
	}
}
package org.lanqiao.service;
 
import org.lanqiao.entity.Student; 

public interface IStudentService {
	public void addStudent(Student student);
}
package org.lanqiao.service.impl;

import org.lanqiao.entity.Student;
import org.lanqiao.mapper.StudentMapper;
import org.lanqiao.service.IStudentService;

public class StudentServiceImpl implements IStudentService{
	private StudentMapper studentMapper;
	
	public void setStudentMapper(StudentMapper studentMapper){
		this.studentMapper = studentMapper;
	}
	
	@Override
	public void addStudent(Student student){
		//   调用 dao 层
		studentMapper.addStudent(student);
	}
}
package org.lanqiao.test;

import org.lanqiao.entity.Student;
import org.lanqiao.service.IStudentService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
	@SuppressWarnings("resource")
	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
		IStudentService studentService =(IStudentService)context.getBean("studentService");
		Student student = new Student();
		student.setStuAge(88);
		student.setStuName("zds");
		student.setStuNo(12);
		studentService.addStudent(student);
	}
}

SM整合-方式二