1. 程式人生 > >SM整合-方式二

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);
	}
}