1.首先要导入的jar包:
至少我 是导入了这么多的jar包 注意:这是一个java项目。
2.首先建立三层模型;
建立dao层,service层,还有实体类entity,然后建立类对其实现接口的对接。
a.对dao层的代码实现:在这里要说明一下 这个接口放在了mapper中是MyBatis的习惯
package org.awen.mapper;
import org.awen.entity.Student;
public interface StudentMapper {
public Student queryStudenyByStuno(int stuno);
public void addStudentDao(Student student);
}
b.对dao层实现类的实现:
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.对接口service的实现:
package org.awen.service;
import org.awen.entity.Student;
public interface IStudentService {
public void addStudent(Student student);
}
d.对service接口的实现:对这个接口的实现有很多细节需要注意。
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.然后做一个测试类:
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);
}
}
具体的放置位置,参考一下。
3.就是一些xml的配置和整合就行了,Spring和MyBatis整合就是把MyBatis的权限交给Spring来处理。
这个算是MyBatis的xml
a.创建一个studentMapper.xml文件,作为数据库操作,注意的是在select中的id必须要和你studentMapper.java中的方法名字是相同的。
要不然是会报错的,就是这个错误搞的我没办法 进行 下面的学习。
<?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>
还有一个conf.xml,这里补充说明一下 ,这个文件是可以省略掉的。
<?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>
这里是spring的配置xml。applicationContext.xml文件,里面有一些详细的解释。之前的数据库信息是在MyBtis中的conf.xml中配置的,但是现在转移过来到spring配置。
<?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>
按照我的方法进行的话,这个是db.properties中的内容。需要配置成你数据库信息的内容,我这边是Sql Server的信息。databaseName =Employees这里面说明的是:数据库的名称。
这个是数据库里面的内容,注意MyBatis的规则,表和类中的属性是要对应起来的,
driver=com.microsoft.sqlserver.jdbc.SQLServerDriver
url=jdbc:sqlserver://localhost:1433;databaseName =Employees
username=sa
password=1228
maxactive=10
maxidle=6
最后就是需要注意的地方:就是数据库的内容了
图片在上面啊。