您的位置:首页 > 编程语言 > Java开发

SPRINGMYBATIS01 Unit08: Spring与MyBatis整合 、 Spring整合MyBatis应用

2017-07-01 09:38 471 查看

1. 返回Map类型的结果

(1) MyBatis会将记录中的数据先放到一个Map对象里面

(以字段名作为key,以字段值作为value,一条记录对应一个Map对象),
然后  再将Map中的数据放到对应的实体对象里面。




(2)返回Map类型的结果,好处是不用实体类了,但是不方便

(因为要获得字段值,还需要调用Map对象提供的get方法,
注意,oracle数据库中,字段名统一都是大写)。


2.解决字段名与实体类的属性名不一致

(1)方式一 使用别名。(就是让别名与属性名一样)。

(2)方式二 使用resultMap解决。



3.Mapper映射器

(1)什么是Mapper映射器?

符合映射文件要求的接口。
注:MyBatis会生成符合该接口要求的对象。


(2)具体要求

a.方法名要与sql的id一致。
b.方法的参数类型要与parameterType一致。
c.方法的返回类型要与resultType一致。
d.映射文件的namespace要等于接口的完整的名字。






(3)如何使用Mapper映射器



4. Spring集成MyBatis (方式一 使用Mapper映射器)

(1)集成步骤

step1.导包。
spring-webmvc,mybatis,mybatis-spring,
dbcp,ojdbc,spring-jdbc,junit。




step2.添加spring配置文件。
注:不再需要MyBatis的配置文件,可以在spring的配置文件里面
添加SqlSessionFactoryBean来代替。




step3.实体类
step4.映射文件
step5.Mapper映射器
step6.配置MapperScannerConfigurer
注:该bean会扫描指定包及其子包下面的所有的Mapper映射器
(即接口),然后调用getMapper方法获得映射器的实现(比如,
调用 EmployeeDAO dao = SqlSession.getMapper(EmployeeDAO.class))。并且,将这些对象添加到Spring容器里面
(默认的id是首字母小写之后的接口名,可以使用@Repository重命名)。




(2)只扫描特定的接口。

step1. 开发一个注解。




step2. 将该注解添加到需要扫描的接口之上。




step3. 修改MapperScannerConfigurer的配置。




演示代码:



src/main/java

annotations

MyBatisRepository.java

package annotations;

public @interface MyBatisRepository {

}


dao

EmployeeDAO.java

package dao;

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

import org.springframework.stereotype.Repository;

import annotations.MyBatisRepository;
import entity.Employee;
import entity.Employee2;

/**
* Mapper映射器
*
*/
@Repository("empDAO")
@MyBatisRepository
public interface EmployeeDAO {
public void save(Employee e);
public List<Employee> findAll();
public Employee findById(int id);
public void modify(Employee e);
public void delete(int id);
public Map findById2(int id);
public Employee2 findById3(int id);
}


entity

Employee.java

package entity;

public class Employee {
private Integer id;
private String name;
private Integer age;

@Override
public String toString() {
return "Employee [id=" + id + ", name=" + name + ", age=" + 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;
}

}


Employee2.java

package entity;

public class Employee2 {
private Integer empNo;
private String ename;
private Integer age;

@Override
public String toString() {
return "Employee2 [empNo=" + empNo + ", ename=" + ename + ", age=" + age + "]";
}

public void setEmpNo(Integer empNo) {
this.empNo = empNo;
}
public void setEname(String ename) {
this.ename = ename;
}
public void setAge(Integer age) {
this.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">

<mapper namespace="dao.EmployeeDAO">
<!--
id:要求唯一
parameterType:参数类型,要写类的完整的名称。
-->
<insert id="save"
parameterType="entity.Employee">
INSERT INTO emp_czh
VALUES(emp_czh_seq.nextval,#{name},#{age})
</insert>

<!--
resultType:返回类型,要写类的完整的名称。
-->
<select id="findAll"
resultType="entity.Employee">
SELECT * FROM emp_czh
</select>

<select id="findById"
parameterType="int"
resultType="entity.Employee">
SELECT * FROM emp_czh
WHERE id = #{id1}
</select>

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

<delete id="delete" parameterType="int">
DELETE FROM emp_czh WHERE id = #{id1}
</delete>

<!-- 返回Map类型的结果 -->
<!--
map是java.util.Map的简写形式
-->
<select id="findById2" parameterType="int"
resultType="map">
SELECT * FROM emp_czh WHERE id = #{id1}
</select>

<!--
resultMap告诉mybatis表的字段名
与实体类的属性名的对应关系。
(如果表的字段名与属性名相同,则不用写了)
-->
<resultMap type="entity.Employee2"
id="empResultMap">
<result property="empNo" column="id"/>
<result property="ename" column="name"/>
</resultMap>

<select id="findById3" parameterType="int"
resultMap="empResultMap">
SELECT * FROM emp_czh WHERE id = #{id1}
</select>
</mapper>


src/mian/resources

db.properties

# db connection parameters
# key=value
driver=oracle.jdbc.driver.OracleDriver
url=jdbc:oracle:thin:@192.168.201.227:1521:orcl
user=openlab
pwd=open123
# datasource parameters
initSize=1
maxSize=1


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.springframewo e817
rk.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"> <!-- 读取db.properties文件 -->
<util:properties id="db"
location="classpath:db.properties"/>
<!-- 配置连接池 -->
<bean id="ds"
class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName"
value="#{db.driver}" />
<property name="url"
value="#{db.url}" />
<property name="username"
value="#{db.user}" />
<property name="password"
value="#{db.pwd}" />
</bean>
<!-- 配置SqlSessionFactoryBean -->
<bean id="ssfb"
class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 注入连接池 -->
<property name="dataSource" ref="ds"/>
<!-- 注入映射文件的位置信息 -->
<property name="mapperLocations"
value="classpath:entity/*.xml"/>
</bean>
<!-- 配置MapperScannerConfigurer -->
<bean
class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 注入要扫描的包名 -->
<property name="basePackage"
value="dao"/>
<!-- 只扫描带有该注解的映射器 -->
<property name="annotationClass"
value="annotations.MyBatisRepository"/>
</bean>
</beans>


src/test/java

test

TestCase.java

package test;

import java.util.List;

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

import dao.EmployeeDAO;
import entity.Employee;

public class TestCase {
private EmployeeDAO dao;
@Before
public void init(){
ApplicationContext ac =
new ClassPathXmlApplicationContext(
"spring-mybatis.xml");
dao = ac.getBean("empDAO",
EmployeeDAO.class);
}
@Test
public void test1(){
List<Employee> emps =
dao.findAll();
System.out.println(emps);
}
@Test
public void test2(){
Employee e = new Employee();
e.setName("Rod Johnson");
e.setAge(40);
dao.save(e);
}
}


pom.xml

<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>3.2.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.8</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.2.2</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>3.2.8.RELEASE</version>
</dependency>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc14</artifactId>
<version>10.2.0.4.0</version>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: