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();

	}

 

posted @ 2016-07-06 00:31  C键盘。夜。  阅读(199)  评论(0编辑  收藏  举报