整合步骤:
1.spring-mybatis.xml
</pre><pre name="code" class="html"><?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-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd" default-autowire="byName" default-lazy-init="false" > <!-- 开启自动注入 --> <context:annotation-config/> <!-- 开启自动扫描,在指定的路径及子路径下扫描,将扫描到的bean注册到spring --> <context:component-scan base-package="org.hnust.cn"/> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/test"/> <property name="username" value="root"/> <property name="password" value="0128"/> </bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 指定要用到的连接池 --> <property name="dataSource" ref="dataSource"/> <!-- 指定mybatis的核心配置文件 --> <property name="configLocation" value="configuration.xml"/> </bean> <bean id="studentMapper" class="org.mybatis.spring.mapper.MapperFactoryBean"> <span style="color:#ff0000;"> <property name="mapperInterface" value="org.hnust.cn.dao.IStudentDao"/></span> <!-- 注入sqlSessionFactory实例 --> <property name="sqlSessionFactory" ref="sqlSessionFactory"/> </bean> </beans>
<?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> <!-- 表明重用预编译的sql语句 --> <settings> <setting name="defaultExecutorType" value="REUSE"/> </settings> <typeAliases> <!-- 学生类型别名定义 --> <typeAlias alias="Student" type="org.hnust.cn.pojo.Student"/> <!-- 老师类型别名定义 --> <typeAlias alias="Teacher" type="org.hnust.cn.pojo.Teacher"/> </typeAliases> <!-- 配置数据库连接环境,default指创建sqlSessionFactory时,若没有明确指定要用哪一个environment,则使用此属性指定的 --> <!-- 与spring整合之后,这一段就不需要 --> <!-- <environments default="development"> <environment id="development"> <transactionManager type="jdbc"/> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/test"/> <property name="username" value="root"/> <property name="password" value="0128"/> </dataSource> </environment> </environments> --> <mappers> <mapper resource="org/hnust/cn/mapping/StudentMapper.xml"/> </mappers> </configuration>3.IStudentDao
package org.hnust.cn.dao; import org.hnust.cn.pojo.Student; public interface IStudentDao { /**** * 根据学生Id查询学生 * @return */ public Student getById(int id); /** * 增加学生信息 * @param student */ public void add(Student student); /*** * 更新学生信息 * @param student */ public void update(Student student); /*** * 删除 * @param id */ public void delete(int id); }4.StudentMapper.xml
<?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 namespace="org.hnust.cn.dao.IStudentDao"> <!-- 数据库表和属性一一对应 --> <resultMap id="studentResultMap" type="Student"> <id property="id" column="id"/> <result property="username" column="username"/> <result property="gender" column="gender"/> <result property="major" column="major"/> <result property="grade" column="grade"/> <!-- javaType属性是必须的 --> <association property="supervisor" javaType="Teacher"> <id property="id" column="id"/> <result property="name" column="name"/> <result property="gender" column="gender"/> <result property="researchArea" column="reseachArea"/> <result property="title" column="title"/> </association> </resultMap> <!-- 查找 --> <select id="getById" parameterType="int" resultMap="studentResultMap"> <!-- select * from student_tab where id=#{id}--> select st.id,st.username,st.major,st.grade,t.id,t.name,t.gender,t.title,t.research_area from student_tab st,teacher_tab t where st.supervisor_id=t.id and st.id=#{id} </select> <!-- 增加 --> <!-- useGeneratedKeys="true" 获得数据库自动生成的主键 keyProperty="id" 指定把获得主键注入到Student的id属性中--> <insert id="add" parameterType="Student" useGeneratedKeys="true" keyProperty="id"> insert into student_tab(username,major,grade,gender) values (#{username},#{major},#{grade},#{gender}) </insert> <!-- 修改学生信息 --> <update id="update" parameterType="Student"> update student_tab set username=#{username}, gender=#{gender},major=#{major},grade=#{grade} where id=#{id} </update> <!-- 删除信息 --> <delete id="delete" parameterType="int"> delete from student_tab where id=#{id} </delete> </mapper>
package org.hnust.cn.pojo; public class Student { private int id; private String username; private String gender; private String major; private String grade; private Teacher supervisor; public Teacher getSupervisor() { return supervisor; } public void setSupervisor(Teacher supervisor) { this.supervisor = supervisor; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } public String getMajor() { return major; } public void setMajor(String major) { this.major = major; } public String getGrade() { return grade; } public void setGrade(String grade) { this.grade = grade; } } package org.hnust.cn.pojo; public class Teacher { private int id; private String name; private String gender; private String researchArea; //研究方向 private String title;//职称 public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } public String getResearchArea() { return researchArea; } public void setResearchArea(String researchArea) { this.researchArea = researchArea; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } }
package org.hnust.cn.test; import org.apache.ibatis.session.SqlSessionFactory; import org.hnust.cn.dao.IStudentDao; import org.hnust.cn.pojo.Student; import org.hnust.cn.util.SqlSessionFactoryGen; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class TestMybatis2 { // private static SqlSessionFactory factory=SqlSessionFactoryGen.getSqlSessionFactory(); 已经被spring注入 private static ApplicationContext ctx; static{ ctx=new ClassPathXmlApplicationContext("spring-mybatis.xml"); } public static void main(String[] args) { IStudentDao studentDao=(IStudentDao) ctx.getBean("studentMapper"); Student student=studentDao.getById(2); StringBuilder sb=new StringBuilder("学生信息:\n"); sb.append("姓名:"); sb.append(student.getUsername()); sb.append("专业:"); sb.append(student.getMajor()+"\n"); sb.append("指导教师:\n"); sb.append(student.getSupervisor().getName()); System.out.println(sb.toString()); } }
中间出现的问题是:
最开始spring-mybatis.xml中我有一段是这样写的:
<bean id="studentMapper" class="org.mybatis.spring.mapper.MapperFactoryBean"> <property name="studentDao" value="org.hnust.cn.dao.IStudentDao"/> <!-- 注入sqlSessionFactory实例 --> <property name="sqlSessionFactory" ref="sqlSessionFactory"/> </bean>
private Class<T> mapperInterface; private boolean addToConfig = true; /** * Sets the mapper interface of the MyBatis mapper * * @param mapperInterface class of the interface */ public void setMapperInterface(Class<T> mapperInterface) { this.mapperInterface = mapperInterface; }
MapperfactoryBean依赖注入的名字是mapperInterface,所以需要把名字改为一致。同理,网上有很多说把sqlSessionFactory改为sqlSessionfactoryBeanName.,,是一样的道理 只是很多人没有写明白原因