2016年2月4日 星期四

MyBatis 3.x 整合 Spring 3.x (基本設定)

官方網站是多語系的

pom.xml

<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis</artifactId>
    <version>3.3.0</version>
</dependency>
    
<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis-spring</artifactId>
    <version>1.2.3</version>
</dependency>
    
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>3.2.13.RELEASE</version>
</dependency>
    
<dependency>
    <groupId>commons-dbcp</groupId>
    <artifactId>commons-dbcp</artifactId>
    <version>1.4</version>
</dependency>
    
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>3.2.13.RELEASE</version>
</dependency>

※下載spring-jdbc是因為我執行出現找不到類別的問題,所以下載這個包



※Annotation設定

applicationContext.xml

<context:component-scan base-package="\" />
    
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
    destroy-method="close">
    <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
    <property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:orcl" />
    <property name="username" value="username" />
    <property name="password" value="password" />
</bean>
    
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
</bean>
    
<bean id="dao" class="org.mybatis.spring.mapper.MapperFactoryBean">
    <property name="mapperInterface" value="dao.IDeptDAO" />
    <property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>



IDeptDAO.java

public interface IDeptDAO {
    @Select("SELECT * FROM DEPT")
    public List<Dept> findAll();
    
// @Select("SELECT * FROM DEPT WHERE DEPTNO = #{pk}")
// public Dept findByPK(@Param("pk") int deptno);
//     
// @Insert("INSERT INTO DEPT(DEPTNO, DNAME, LOC) VALUES (60, 'aaa', 'bbb')")
// public void insert();
//     
// @Update("UPDATE DEPT SET DNAME = 'AAA', LOC = 'BBB' WHERE DEPTNO = 60")
// public void update();
//     
// @Delete("DELETE FROM DEPT WHERE DEPTNO = 60")
// public void delete();
}



測試類

ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
    
for (String s : ctx.getBeanDefinitionNames()) {
    System.out.println(s);
}
    
IDeptDAO mfb = (IDeptDAO) ctx.getBean("dao");
List<Dept> list = mfb.findAll();
for (Dept dept : list) {
    System.out.println("deptNo=" + dept.getDeptNo());
    System.out.println("dName=" + dept.getdName());
    System.out.println("loc=" + dept.getLoc());
    System.out.println();
}
    
ctx.close();

※因為有sqlSessionFactory,所以dao可以直接強轉

※在 Service 寫 private IDeptDAO xxx; 然後 setter,最後上面放注入的 annotation 即可抓到


※XML設定

DeptDaoImpl.java

@Repository
public class DeptDaoImpl implements IDeptDAO {
    @Autowired
    public SqlSessionTemplate sqlSession;
    
    public List<Dept> findAll() {
        return sqlSession.selectList("xxx.getDept");
    }
}

※XML設定比較麻煩,要多個實作類別,還要有設定檔(mybatis-config.xml)或mapping檔(Dept.xml)


applicationContext.xml

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <!-- <property name="configLocation" value="classpath:mybatis-config.xml" /> -->
    <property name="mapperLocations" value="classpath:Dept.xml" />
</bean>
    
<bean id="dao" class="org.mybatis.spring.SqlSessionTemplate">
    <constructor-arg index="0" ref="sqlSessionFactory" />
</bean>

※將dao換成SqlSessionTemplate,當然dataSource還是要,因為沒變就不寫了

※sqlSessionFactory增加屬性,configLocation和mapperLocations大部分的情況選其一即可,因為mybatis-config.xml可以如下設定:
<mappers>
    <mapper resource="Dept.xml" />
</mappers>

※因為目前的設定還不需要,所以我選擇用mapperLocations就夠了



Dept.xml

<mapper namespace="xxx">
    <select id="getDept" resultType="vo.Dept">
        SELECT * FROM DEPT
    </select>
</mapper>

※DeptDaoImpl.java的字串要namespace.id,和沒整合前一樣,沒什麼特別


測試類

IDeptDAO mfb = (IDeptDAO) ctx.getBean("deptDaoImpl");

※getBean就選新加的class就搞定了

沒有留言:

張貼留言