Integration of small case columns of Spring and MyBatis

1. First import jar package:


At least I was introduced so many jar package Note: This is a java project.

2. First establish three model;
establish dao layer, service layer, as well as the entity class entity, and then build the docking of its class that implements the interface.
. a code dao layer to achieve: in here to explain this interface is placed mapper MyBatis habit

package org.awen.mapper;

import org.awen.entity.Student;

public interface StudentMapper {
public Student queryStudenyByStuno(int stuno);
public void addStudentDao(Student student);
}

. B dao layer implementation of a class that implements:

package org.awen.daoimpl;

import java.io.Reader;
import java.util.List;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.awen.entity.Student;
import org.awen.mapper.StudentMapper;
import org.mybatis.spring.support.SqlSessionDaoSupport;

/*继承这个包 实现 对MyBatis的实现*/
public class StudentDaoImpl extends SqlSessionDaoSupport implements StudentMapper{

@Override
/***
 *	
 *Reader reader =Resources.getResourceAsReader("conf.xml");
	//reader -SqlSession
	SqlSessionFactory sessionFacotory=new SqlSessionFactoryBuilder().build(reader);
	SqlSession session=sessionFacotory.openSession();
	StudentMapper studentMapper=session.getMapper(StudentMapper.class);
	List<Student> students =studentMapper.queryStudenyOrderByColumn("stuName");
	
	System.out.println(students);
	session.close();
	在dao层执行这些代码 是对数据库的操作 很正确 对数据库的操作 
	只有在到层实现的
 * 
 **/
public void addStudentDao(Student student) {
	// TODO 自动生成的方法存根
	//这里的步骤就是和上面的MyBatis 中的步骤是差不多相同的了
	SqlSession session=super.getSqlSession();
	//这是一个接口
	StudentMapper stuDao=session.getMapper(StudentMapper.class);
	//这一步就是差不多在调用 那些接口中的方法
	stuDao.addStudentDao(student);
}

@Override
public Student queryStudenyByStuno(int stuno) {
	// TODO 自动生成的方法存根
	return null;
}

}

. c realize the service interfaces:
Package org.awen.service;

import org.awen.entity.Student;

public interface IStudentService {
	public void addStudent(Student student);
}

d to achieve service interface: There are many details to pay attention to the implementation of this interface.
package org.awen.serviceimpl;

import org.awen.daoimpl.StudentDaoImpl;
import org.awen.entity.Student;
import org.awen.mapper.StudentMapper;
import org.awen.service.IStudentService;

public class StudentServiceImpl implements IStudentService{

StudentMapper studentMapper;


public void setStudentMapper(StudentMapper studentMapper) {
	this.studentMapper = studentMapper;
}

@Override
public void addStudent(Student student) {
	// TODO 自动生成的方法存根
	//调用Dao
	studentMapper.addStudentDao(student);
}

}

. e then do a test class:
Package org.awen.test;

import org.awen.entity.Student;
import org.awen.service.IStudentService;
import org.awen.serviceimpl.StudentServiceImpl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class test {
	public static void main(String args[]) {
		ApplicationContext contet=new ClassPathXmlApplicationContext("applicationContext.xml");
		IStudentService StudentService=(IStudentService)contet.getBean("StudentServiceImpl");
		Student student =new Student();
		student.setStuAge(88);
		student.setStuName("as");
		student.setStuNo(100);
		StudentService.addStudent(student);
	}
}

Specific placement, refer to.
Here Insert Picture Description

3. that some xml configuration and integration on the line, Spring and MyBatis MyBatis of integration is the authority to Spring to deal with.

This is regarded as MyBatis xml

a. Create a studentMapper.xml file as a database operation, and must be noted that the method name you studentMapper.java is the same as the id of the select.
Or else is being given, that is out of this error I can not carry out the next steps.

<?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">
 <!-- namespace:该mapper.xml映射文件的 唯一标识 -->
 <mapper namespace="org.awen.mapper.StudentMapper">
 
 
 	<select id="queryStudenyByStuno" resultType="org.awen.entity.Student" parameterType="int">
 		select * from student where stuNo =#{stuNo}
 	</select>
 	
 	
 	<!--  -->
 	<insert id="addStudentDao" parameterType="org.awen.entity.Student">
 		insert into student(stuNo,stuName,stuAge) values(#{stuNo},#{stuName},#{stuAge})
 	
 	</insert>

 
 </mapper>

There is also a conf.xml, where a side note, this file can be omitted's.

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
 PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
 "http://mybatis.org/dtd/mybatis-3-config.dtd">
 <configuration>

 
 		<mappers>
		 <!-- 加载映射文件 -->
		 <mapper resource="org/awen/mapper/studentMapper.xml"/>
	 </mappers>
 </configuration>

Here is the spring configuration xml. applicationContext.xml file, there are some detailed explanation. Before the database information is configured in MyBtis in conf.xml in, but now transferred over to the spring configuration.

<?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:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx.xsd">

	<bean id="config" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
	<!-- 导入db.properties文件 -->
		<property name="locations">
			<array>
				<value>
				classpath:db.properties
				</value>			
			</array>
		</property>
		
	</bean>
	
	<!-- 第一种方式生成mapper对象 -->
	<bean id="studentdaoimplid"  class="org.awen.daoimpl.StudentDaoImpl">
	<!-- 虽然Dao层里面没有任何属性,但是在其继承类中有这个属性
	将spring配置的SQL Session Factory对象交给mapper(dao) -->
		<property name="sqlSessionFactory" ref="SqlSessionFactory"></property>
	</bean>
	


	
	<bean id="StudentServiceImpl" class="org.awen.serviceimpl.StudentServiceImpl">
		<!-- 适合StudentServiceImpl中的属性名是相同的。 -->
		<property name="studentMapper" ref="studentMapper"></property>
	
	</bean>
	

	
	<!-- 配置数据库相关-事务
	如果name里面报错那么久说明了 这个类中不包含这个属性 -->
	<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>
		<property name="maxActive" value="${maxactive}"></property>
		<property name="maxIdle" value="${maxidle}"></property>
		
	</bean>
	

	<!-- 在SPringIOC中 创建Mybatis得核心类SqlSessionFactory 
	MyBatis的核心SqlSesseionFactory将其交给Dao层 ,才可以使用-->
	<bean id="SqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource"></property>
		
		<!-- 加载MyBatis文件 conf.xml 将mybatis中的conf.xml中的权限给spring
		<property name="configLocation" value="classpath:conf.xml"></property>
		-->
		
		<!-- 记载mapper.xml路径 org/awen/mapper/*.xml一次性将所有的xml文件添加到其中-->
		<property name="mapperLocations" value="org/awen/mapper/studentMapper.xml"></property>
	
	</bean>
	
	
	
</beans>

Goes according to my method, this is the content of db.properties. What you need to configure the database information, the information I have here is the Sql Server. databaseName = Employees there noted: name of the database.

The content inside the database, attention MyBatis rules, tables, and attributes to the class of the association,
Here Insert Picture Description

driver=com.microsoft.sqlserver.jdbc.SQLServerDriver
url=jdbc:sqlserver://localhost:1433;databaseName =Employees
username=sa
password=1228
maxactive=10
maxidle=6

The last place that needs attention: the contents of the database is
in the picture above ah.

Guess you like

Origin blog.csdn.net/guoguozgw/article/details/91493884
Recommended