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、测试

用第六步的测试类,再次测试一次。

发布了23 篇原创文章 · 获赞 5 · 访问量 1468

猜你喜欢

转载自blog.csdn.net/zj499063104/article/details/100115626