spring,mybatis整合时出现的一个小问题
来源:互联网 发布:淘宝旺旺可以改名字吗 编辑:程序博客网 时间:2024/06/05 20:05
整合步骤:
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>
2.configuration.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><!-- 表明重用预编译的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>
5.Student,Teacher
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>
最终测试的时候始终报错,无法生成接口的实例,后来通过查看MapperfactoryBean的源码,发现这么一段:
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.,,是一样的道理 只是很多人没有写明白原因
0 0
- spring,mybatis整合时出现的一个小问题
- Spring + Mybatis + Struts2 整合时出现的问题
- spring mybatis整合框架时由于mybatis版本与mysql客户端不匹配出现的问题
- spring 整合mybatis 出现解析不了配置文件的问题
- Spring 整合mybatis时遇到的问题
- mybatis整合struts,spring的一个细节问题
- spring和mybatis整合时,出现不能连接到jdbc的问题
- Struts2+Spring+Hibernate整合的一个小问题
- SpringMvc+Mybatis整合hbase时出现的问题
- springmvc和mybatis整合出现的问题
- 测试Spring和Hibernate整合时,出现的一个问题记录
- spring与mybatis整合时mapper的配置问题
- 关于Spring整合mybatis时配置MapperScannerConfigurer的问题
- mybatis与spring整合时读取properties问题的解决
- maven整合ssm时mybatis-Spring的版本问题
- Spring、SpringMVC、Mybatis整合遇到的问题
- 解决spring+Mybatis的整合测试问题
- spring+springmc+mybatis整合遇见的问题
- Java final以及static用法简单demo
- android-Saving Data
- Ubuntu下为SD卡分区
- PHP中的set_time_limit,max_execution_time,sleep
- Unity2d 移动设备的横竖屏适配及UGUI画布调整使Editor与真实设备的UI显示保持一致
- spring,mybatis整合时出现的一个小问题
- 多线程的使用(1)-performSelectorOnMainThread
- iOS 中KVC、KVO、NSNotification、delegate 总结及区别
- php学习02--常量与变量
- 本地服务(local Service)的实现
- Hadoop初次接触后感
- Mysql 查询
- expdp和impdp需要注意的地方
- 拟阵理论简述