Spring統合mybatisクエリーと更新例


spring+mybatis+mysql
mybatis jarパッケージhttps://code.google.com/p/mybatis/downloads/list?can=3&q=Product%3DMyBatis
mybatis統合springjarパッケージhttps://code.google.com/p/mybatis/downloads/list?can=3&q=Product%3DSpring
まずデータベースを作成
CREATE TABLE emp (
  id int)  auto_increment,
  name varchar(20) NOT NULL,
  age int NOT NULL,
  PRIMARY KEY  (id)
) ENGINE=INNODB;

Webプロジェクトを新規作成
データベース・エンティティ・クラスの作成
package entity;
import java.io.Serializable;
public class Emp implements Serializable {
    private static final long serialVersionUID = 1311811898794089205L;
                                                                                                                                                                                                                                                                             
    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;
    }
}

EmpDaoの作成(インタフェース)
package dao;
import entity.Emp;
public interface EmpDao {
    public Emp getById(Integer id);
                                                                                                                                                                                                                                                  
    public void update(Emp emp);
}

mybatisプロファイルを作成します(名前はここでmybatis-config.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> 
    <typeAliases >
        <typeAlias type="entity.Emp" alias="emp"/><!--         -->
    </typeAliases>
    <mappers>
        <mapper resource="mapper/EmpDaoMapper.xml"/> <!--         -->
    </mappers>
</configuration>

EmpDaoMapperを作成します.xml(EmpDaoプロファイル)
<?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="dao.EmpDao"><!--   dao   -->
    <select id="getById" parameterType="Integer" resultType="entity.Emp">
        <![CDATA[
            select * from emp where id=#{id}
        ]]>
    </select>
                                                                                                                                                                                      
    <update id="update" parameterType="emp"><!-- emp   Emp             (      mybatis-config.xml)    -->
        <![CDATA[
            update emp set name=#{name},age=#{age} where id=#{id}
        ]]>
    </update>
</mapper>

EmpService(インタフェース)の作成
package service;
import entity.Emp;
public interface EmpService {
    public Emp getById(Integer id);
                                                                                                                                                                                                              
    public void update(Emp emp);
}

EmpServiceImplの作成(EmpService実装クラス)
package service.impl;
import dao.EmpDao;
import entity.Emp;
import service.EmpService;
public class EmpServiceImpl implements EmpService {
    private EmpDao empDao;
    @Override
    public Emp getById(Integer id) {
        return empDao.getById(id);
    }
    public void setEmpDao(EmpDao empDao) {
        this.empDao = empDao;
    }
    @Override
    public void update(Emp emp) {
        empDao.update(emp);
    }
                                                                   
}

スプリングプロファイルの作成
<?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"
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd
    ">
                                                      
    <!--     context            jdbc     -->
    <context:property-placeholder location="classpath:mysql.properties"/>
                                                      
    <!--           jar  -->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${driverClassName}"/>
        <property name="url" value="${url}"/>
        <property name="username" value="${username}"/>
        <property name="password" value="${password}"/>
    </bean>
                                                       
    <!--            (dbcp  )     commons-pool.jar   commons-dbcp.jar
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="${driverClassName}"/>
        <property name="url" value="${url}"/>
        <property name="username" value="${username}"/>
        <property name="password" value="${password}"/>
    </bean>
     -->
                                                      
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
        <property name="dataSource" ref="dataSource"/>
    </bean>
                                                      
                                                      
    <bean id="empDao" class="org.mybatis.spring.mapper.MapperFactoryBean">
        <property name="mapperInterface" value="dao.EmpDao"/>
        <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
    </bean>
                                                      
    <bean id="empService" class="service.impl.EmpServiceImpl">
        <property name="empDao" ref="empDao"/>
    </bean>
</beans>

データ・ソースを構成する場合、プロパティ・ファイルのフィールドを参照して新しいプロパティ・ファイルを作成できます.
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/xx
username=root
password=

テスト
package test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import entity.Emp;
import service.EmpService;
public class TestMyBatis {
    public static void main(String[] args) {
        ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
        EmpService empService= (EmpService) ac.getBean("empService");
        Emp emp=empService.getById(1);
        System.out.println(emp.getName());
        emp.setName("  2");
        empService.update(emp);
        emp=empService.getById(1);
        System.out.println("   "+emp.getName());
    }
}

結果
  
     2