Mybatis入门——Spring整合MyBatis

Spring整合MyBatis

对Teacher表进行添加数据和查看数据

1. 创建数据库表

CREATE TABLE `teacher` (
  `t_id` varchar(15) NOT NULL,
  `t_name` varchar(30) DEFAULT NULL,
  PRIMARY KEY (`t_id`)
)

2.创建Spring工程

3.导入相关包

mybatis-spring-1.3.0.jar

mybatis-3.3.0.jar

mysql-connector-java-5.1.7-bin.jar

以及Spring相关jar包

4.配置applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans>

	<context:component-scan base-package="com">
	</context:component-scan>

	<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
		<property name="url" value="jdbc:mysql://127.0.0.1/test"></property>
		<property name="username" value="root"></property>
		<property name="password" value="1234"></property>
	</bean>
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource"></property>
	</bean>

	<bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="com.mapper"></property>
		<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
	</bean>

	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" >
		<property name="dataSource" ref="dataSource"></property>
	</bean>
	<tx:annotation-driven transaction-manager="transactionManager"/>

</beans>

5.创建TeacherBean.java

package com.bean;

import org.springframework.stereotype.Component;

@Component
public class TeacherBean {

    private String tId;
    private String tName;
    public String gettId() {
        return tId;
    }
    public void settId(String tId) {
        this.tId = tId;
    }
    public String gettName() {
        return tName;
    }
    public void settName(String tName) {
        this.tName = tName;
    }
    public TeacherBean(String tId, String tName) {
        super();
        this.tId = tId;
        this.tName = tName;
    }
    public TeacherBean() {
        super();
    }
    @Override
    public String toString() {
        return "TeacherBean [tId=" + tId + ", tName=" + tName + "]";
    }

}

6.创建对Teacher表进行操作的Mapper接口

TeacherCrudMapper.java

package com.mapper;

import com.bean.TeacherBean;

public interface TeacherCrudMapper {
	public TeacherBean getTeacherInfor(String tId);
	public boolean insertTeacher(TeacherBean teacher);

}

7.创建接口对应的mapping xml文件

teacherCrudMapper.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">
<mapper namespace="com.mapper.TeacherCrudMapper">//namespace应与接口名完全一致
    <select id="getTeacherInfor" parameterType="String" resultMap="getTeacherMap">
        select * from teacher where t_id=#{tId};
    </select>
    <resultMap type="com.bean.TeacherBean" id="getTeacherMap">
        <id property="tId" column="t_id"/>
        <result property="tName" column="t_name"/>
    </resultMap>
    <insert id="insertTeacher" parameterType="com.bean.TeacherBean">
        insert into teacher values(#{tId},#{tName});
    </insert>
</mapper>

8.对teacher表进行操作

package com.dao;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.bean.TeacherBean;
import com.mapper.TeacherCrudMapper;

@Component
public class TeacherCrud {

	@Autowired
	private TeacherCrudMapper teacherCrudMapper;

	public TeacherBean getTeacher(String tId)
	{
		TeacherBean tBean=teacherCrudMapper.getTeacherInfor(tId);
		return tBean;
	}
	public boolean insertTeacher(TeacherBean teacherBean)
	{
		return teacherCrudMapper.insertTeacher(teacherBean);
	}

}

9.调用Main函数

package com;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.bpo.TeacherBpo;

public class Main {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		ApplicationContext context= new ClassPathXmlApplicationContext("applicationContext.xml");
		TeacherBpo tb=(TeacherBpo) context.getBean("teacherBpo");
		tb.TeacherScan();
		tb.TeacherInsert();

	}
时间: 2024-10-17 19:41:21

Mybatis入门——Spring整合MyBatis的相关文章

Mybatis和Spring整合&amp;逆向工程

Mybatis和Spring整合&逆向工程Mybatis和Spring整合mybatis整合Spring的思路目的就是将在SqlMapConfig.xml中的配置移植到Spring的applicationContext.xml文件中让spring管理SqlSessionFactory让spring管理mapper对象和dao.使用spring和mybatis整合开发mapper代理及原始dao接口.自动开启事务,自动关闭 sqlsession.让spring管理数据源( 数据库连接池)导入相关的

Mybatis与Spring整合

Mybatis与Spring整合无外乎要将数据源,以及事物管理的相关配置交给spring来管理,mybatis配置负责sqlmapper的相关配置也就是dao层到sql映射的相关配置. 一下以手机管理系统dao层实现所用到的Spring与MyBatis整合为例. 1.spring中beans.xml相关配置 <!--加载数据源基本配置文件--><context:property-placeholder location="classpath:conf/jdbc.properti

spring整合mybatis遇到的bug java.lang.IllegalArgumentException: Property &#39;sqlSessionFactory&#39; or &#39;sqlSessionTemplate&#39; are required

出bug的原因:mybatis-spring版本问题. 查看SqlSessionDaoSupport源码 1.2以上的版本: 1.1.1版本: 解决方法:1.2版本移除了@Autowired的注解,所以如果是1.2版本以上,要在BaseDaoImpl里面手动 注入SetSessionTemplate或者SetSessionFactory spring整合mybatis遇到的bug java.lang.IllegalArgumentException: Property 'sqlSessionFa

Spring整合Mybatis应用

1.整合步骤介绍 基于SpringMVC和Mybatis技术开发的主要步骤如下 1>创建工程,搭建SpringMVC和Mybatis技术环境 2>基于MapperScannerConfigurer方式整合Mybatis的Mapper接口(推荐) 3>编写和配置SpringMVC的主要组件,例如Controller,HandlerMapping,ViewResolver等 4>编写JSP视图组件,利用标签和表达式显示模型数据 5>测试程序 2.如何搭建SpringMVC和Myb

Spring整合Mybatis解决 Property &#39;sqlSessionFactory&#39; or &#39;sqlSessionTemplate&#39; are required

在Spring4和Mybatis3整合的时候,dao层注入'sqlSessionFactory'或'sqlSessionTemplate'会报错解决办法如下: package com.alibaba.webx.MyWebxTest.myWebX.module.dao.impl; import org.apache.ibatis.session.SqlSessionFactory; import org.mybatis.spring.SqlSessionTemplate; import org.m

spring 整合Mybatis 《错误集合,总结更新》

错误:nested exception is java.lang.NoClassDefFoundError: org/aopalliance/intercept/MethodInterceptor  运行环境:jdk1.7.0_17 + tomcat 7 + eclipse spring整合mybatis启动时候出现這个错误: SEVERE: Exception sending context initialized event to listener instance of class org

mybatis与spring整合时读取properties问题的解决

在学习mybatis与spring整合是,想从外部引用一个db.properties数据库配置文件,在配置文件中使用占位符进行引用,如下: 1 <context:property-placeholder location="classpath:db.properties" /> 2 <bean id="dataSource" 3 class="org.springframework.jdbc.datasource.DriverManage

spring源码剖析(八)spring整合mybatis原理

前言 MyBatis相信很多人都会使用,但是当MyBatis整合到了Spring中,我们发现在Spring中使用更加方便了.例如获取Dao的实例,在Spring的我们只需要使用注入的方式就可以了使用Dao了,完全不需要调用SqlSession的getMapper方法去获取Dao的实例,更不需要我们去管理SqlSessionFactory,也不需要去创建SqlSession之类的了,对于插入操作也不需要我们commit. 既然那么方便,Spring到底为我们做了哪些工作呢,它如何将MyBatis整

spring整合mybatis错误:class path resource [config/spring/springmvc.xml] cannot be opened because it does not exist

spring 整合Mybatis 运行环境:jdk1.7.0_17+tomcat 7 + spring:3.2.0 +mybatis:3.2.7+ eclipse 错误:class path resource [config/spring/springmvc.xml] cannot be opened because it does not exist 错误原因:找不到我的springmvc.xml,在下面web.xml中是我引用路径,网上找到问题classpath指向路径不是resource路