SSM框架(Spring + SpringMVC + MyBatis)學習筆記(03),第四課:MyBatis

spring + mybatis 實現步驟圖

在這裏插入圖片描述

一、配置環境,添加jar包

jar包主要包含ioc, aop, dao, 連接池dbcp, mybatis-spring, mybatis驅動包。
自己在學習的時候發現所有的配置以及代碼都是正確的,但是運行會報錯,問題出在jar包的版本上。各個jar包的版本之間的兼容程度不一樣可能會導致報錯,包括jdk的版本。所以各位小夥伴在練習的時候注意一下。

二、配置applicationContext.xml

applicationContext.xml文件中包含:
第六步,配置DataSource DBCP
第七步,配置SqlSession FactoryBean
第八步,配置MapperScanner Configurer

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

<!-- 定義dbcp的DataSource -->
<bean id="dbcp" class="org.apache.commons.dbcp.BasicDataSource">
	<property name="username" value="root"></property>
	<property name="password" value="asdf123456"></property>
	<property name="url" value="jdbc:mysql://localhost:3306/test?useUnicode=true&amp;characterEncoding=utf8"></property>
	<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
</bean>

<!-- 創建sqlSessionFactory -->
<bean id="ssf" class="org.mybatis.spring.SqlSessionFactoryBean">
	<!-- 注入dataSource -->
	<property name="dataSource" ref="dbcp">
	</property>
	<!-- 注入SQL語句文件 -->
	<property name="mapperLocations" value="classpath:cn/springmybatis02/sql/*.xml">
	</property>
</bean>

<!-- 可以根據給定的Mapper接口生成實現組件 -->
<!-- 指定mapper接口 -->
<!-- 指定sqlsession資源 -->
<!-- 
<bean id="empDao" class="org.mybatis.spring.mapper.MapperFactoryBean">
	
	<property name="mapperInterface" value="cn.springmybatis02.dao.EmpDao">
	</property>
	
	<property name="sqlSessionFactory" ref="ssf">
	</property>
</bean>
 -->

<!-- 可以根據指定路徑批量生成Dao實現 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
	<!-- 掃描cn.springmybatis02.dao包下所有接口,批量生成實現 -->
	<property name="basePackage" value="cn.springmybatis02.dao">
	</property>
	<!-- 此處可以省略,自動注入sqlsessionfactory -->
	<!-- 
	<property name="sqlSessionFactory" ref="ssf">
	</property>
	 -->
</bean>

</beans>

三、編寫實體類

根據自己的實際情況而定是否有必要編寫實體類,本學習筆記中實體類的作用是做爲sql的返回值類型而存在的。

package cn.springmybatis02.entity;

import java.io.Serializable;

public class Emp implements Serializable{
	
	public Emp() {
		
	}
	
	public Emp(String name, Double salary) {
		super();
		this.name = name;
		this.salary = salary;
	}
	public Emp(String name, Integer sex, Double salary) {
		super();
		this.name = name;
		this.sex = sex;
		this.salary = salary;
	}
	private Integer id;
	private String name;
	private Integer sex;
	private Double salary;
	
	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 getSex() {
		return sex;
	}
	public void setSex(Integer sex) {
		this.sex = sex;
	}
	public Double getSalary() {
		return salary;
	}
	public void setSalary(Double salary) {
		this.salary = salary;
	}
	
}

四、定義SQL文件

<?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="cn.springmybatis02.dao.EmpDao">
	
	<select id="findAll" resultType="cn.springmybatis02.entity.Emp">
		select * from emp
	</select>
	
</mapper>

五、定義dao接口

package cn.springmybatis02.dao;

import java.util.List;

import cn.springmybatis02.entity.Emp;

public interface EmpDao {
	
	public List<Emp> findAll();
}

六、編寫測試類

package cn.springmybatis02.test;

import java.util.List;

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

import cn.springmybatis02.dao.EmpDao;
import cn.springmybatis02.entity.Emp;

public class EmpDaoTest {
	
	public static void main(String[] args) {
		
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		EmpDao empDao = ac.getBean("empDao",EmpDao.class);
		List<Emp> list = empDao.findAll();
		for(Emp emp : list) {
			System.out.println("name:"+emp.getName()+", sex:"+emp.getSex()+", salary:"+emp.getSalary());
		}
	}

}

七、其他知識,自定義註解標記

1、自定義註解標記類
package cn.springmybatis02.annotation;

/**
 * 此類是一個註解定義類,
 * 表示只有在添加@MyBatisDao註解的情況下才能實現某種功能
 *
 */
public @interface MyBatisDao {

}

2、定義使用地方

在生成dao接口實現的時候增加限制條件,只有dao接口類前添加了@MyBatisDao註解標記類才能夠生成dao接口實現。

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

<!-- 定義dbcp的DataSource -->
<bean id="dbcp" class="org.apache.commons.dbcp.BasicDataSource">
	<property name="username" value="root"></property>
	<property name="password" value="asdf123456"></property>
	<property name="url" value="jdbc:mysql://localhost:3306/test?useUnicode=true&amp;characterEncoding=utf8"></property>
	<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
</bean>

<!-- 創建sqlSessionFactory -->
<bean id="ssf" class="org.mybatis.spring.SqlSessionFactoryBean">
	<!-- 注入dataSource -->
	<property name="dataSource" ref="dbcp">
	</property>
	<!-- 注入SQL語句文件 -->
	<property name="mapperLocations" value="classpath:cn/springmybatis02/sql/*.xml">
	</property>
</bean>

<!-- 可以根據給定的Mapper接口生成實現組件 -->
<!-- 指定mapper接口 -->
<!-- 指定sqlsession資源 -->
<!-- 
<bean id="empDao" class="org.mybatis.spring.mapper.MapperFactoryBean">
	
	<property name="mapperInterface" value="cn.springmybatis02.dao.EmpDao">
	</property>
	
	<property name="sqlSessionFactory" ref="ssf">
	</property>
</bean>
 -->

<!-- 可以根據指定路徑批量生成Dao實現 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
	<!-- 掃描cn.springmybatis02.dao包下所有接口,批量生成實現 -->
	<property name="basePackage" value="cn.springmybatis02.dao">
	</property>
	
	<!-- 只有添加自定義的註解標記的類,才能生成Dao實現 -->
	<property name="annotationClass" value="cn.springmybatis02.annotation.MyBatisDao">
	</property>
	
	<!-- 此處可以省略,自動注入sqlsessionfactory -->
	<!-- 
	<property name="sqlSessionFactory" ref="ssf">
	</property>
	 -->
</bean>

</beans>

3、添加註解標記
爲需要生成dao接口實現的類添加自定義的註解標記@MyBatisDao

package cn.springmybatis02.dao;

import java.util.List;

import cn.springmybatis02.annotation.MyBatisDao;
import cn.springmybatis02.entity.Emp;

@MyBatisDao
public interface EmpDao {
	
	public List<Emp> findAll();
}

4、測試

用第六步的測試類,再次測試一次。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章