springji集成mybatis

dao包下EmpDao类

package dao;

import java.util.List;
import java.util.Map;

import annotation.MyBatisRepository;
import entity.Emp;
import entity.Emp2;

/**
 *Mapper接口 
 *a.    方法名与映射文件中  sql的id 一样
b.    参数类型与sql定义中的参数一致
c.    方法的返回类型与sql定义中的返回类型一致
 */
//添加自定义注解@MyBatisRepository
@MyBatisRepository
public interface EmpDAO {
        public void save(Emp emp);        
        public  List<Emp> findAll();    
        public Emp findById(int id);
        public void modify(Emp emp);
        public void delete(int id);
        public Map  findById2(int id);
        public Emp2 findById3(int id);
        
        
        
}
 

======================entity下Emp类============================

package entity;

/**
 *实体类的属性名必须与表的字段名一样,名称相同,大小写无所谓 
 *
 */
public class Emp {
    private Integer id;
    private String name;
    private Integer age;
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
    @Override
    public String toString() {
        return "Emp [id=" + id + ", name=" + name + ", age=" + age + "]";
    }
    
    
}
 

-----------------------------------------------------映射文件EmpMapper.xml----------------------------------------------------------

<?xml version="1.0" encoding="UTF-8" ?>  
<!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN"      
 "http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">

<!-- 命名空间namespace("test")用来区分同名的sql id在同一个文件里面唯一。 #{name},
name是Emp的属性 ,mybatis会利用反射机制 读取emp对象的属性值 -->
<!-- 使用Mapper映射器,namespace必须等于Mapper接口的完整的名字,包名+类名 -->
<!-- 没使用映射之前namespace="test" -->
<mapper namespace="dao.EmpDAO">
    <!-- id要求唯一,parameterType表示插入参数对象的类型 -->
    <insert id="save" parameterType="entity.Emp">
        INSERT INTO emp1(id,name,age)
        VALUES(seq_emp_id.NEXTVAL,#{name},#{age})
    </insert>

    <!-- resultType是返回的结果的类型 -->
    <select id="findAll" resultType="entity.Emp">
        SELECT id ,name,age FROM emp1

    </select>

    <!-- 下面的id可以写任意名字都行,因为,没有读对象的属性 -->
    <select id="findById" parameterType="java.lang.Integer"
        resultType="entity.Emp">
        SELECT * FROM emp1 WHERE id= #{id1}
    </select>


    <update id="modify" parameterType="entity.Emp">
        UPDATE emp1 SET
        name=#{name},age=#{age} WHERE id=#{id}
    </update>

    <!-- 下面的id可以写任意名字 -->
    <delete id="delete" parameterType="int">
        DELETE FROM emp1 WHERE
        id=#{id1}
    </delete>

    <!-- 返回Map类型的值 java.util.Map可以简化为map -->
    <select id="findById2" parameterType="int" resultType="map">
        SELECT *
        FROM emp1 WHERE id= #{id3}
    </select>

    <!-- 解决属性名字与字段名不一致的情况 -->
    <select id="findById3" parameterType="int" resultMap="emp2Map">
        SELECT *
        FROM emp1 WHERE id= #{id3}
    </select>
    <!-- 告诉mybatis怎么将字段名和实体类名对应, 只需要将不一样的对应起来 property 是实体类的属性名,column是表中的字段名 -->
    <resultMap type="entity.Emp2" id="emp2Map">
        <result property="empNo" column="id" />
        <result property="ename" column="name" />
    </resultMap>

</mapper>

 

----------------------------------------resource包下spring-mybatis.xml文件---------------------------------------------------

<?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:jdbc="http://www.springframework.org/schema/jdbc"  
    xmlns:jee="http://www.springframework.org/schema/jee" 
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
        http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">

    <util:properties id="jdbc" location="classpath:db.properties"></util:properties>
    <!-- 配置DataSource -->
    <bean name="ds"
        class="org.apache.commons.dbcp.BasicDataSource"
            destroy-method="close">
        <property name="driverClassName" value="#{jdbc.driver}"></property>
        <property name="url" value="#{jdbc.url}"></property>
        <property name="username" value="#{jdbc.user}"></property>
        <property name="password" value="#{jdbc.password}"></property>
    </bean>
    
    <!-- 配置SqlSessionFactoryBean
    作用:为mybatis提供:1.如何连数据库(数据源的配置)2.映射文件位置 -->
    <bean id="ssfb" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 指定连接数据源 -->
        <property name="dataSource" ref="ds"></property>
        <!-- 指定映射文件的位置 -->
        <property name="mapperLocations" value="classpath:entity/*.xml"></property>            
    </bean>
    
    <!--配置MapperScannerConfigurer(指定扫描的包)
    负责将mybatis生成的Mapper对象放到spring容器里面
    Mapper对象:SqlSession.getMapper()返回值,即Mapper接口的实现
    -->
        <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        
             <!--指定Mapper映射器所在的包 -->
            <property name="basePackage" value="dao"/>
            <!-- 只扫描带有特定注解的Mapper接口 -->
            <property name="annotationClass" value="annotation.MyBatisRepository"></property>            
        </bean>
    
</beans>
 

----------------------------------------------测试类--------------------------------

package test;

import java.util.List;

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

import dao.EmpDAO;
import entity.Emp;

public class TestCase {
        @Test
        public void test1() {
            //测试spring集成mybatis启动spring容器
            ApplicationContext ac=new ClassPathXmlApplicationContext("spring-mybatis.xml");
            
            //id默认是类名"EmpDAO"首字母小写empDAO
            EmpDAO dao=ac.getBean("empDAO",EmpDAO.class);
                 List<Emp> emps=dao.findAll();
            System.out.println(emps);
            
        }
        
    
}
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值