1、项目结构图
2、jar包
1、MySQL的数据库驱动的版本要5.1.46及以上
2、红色框框的是必备的jar包
spring配置文件
beans.xml
1、占位符${},不要写错
2、xml文件的<?xml version="1.0" encoding="UTF-8"?> 要保证在第一行,不然会报错
3、driver、url间不能有空格
4、数据库username及password要写对
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<!-- 加载do.properties文件 -->
<context:property-placeholder location="classpath:do.properties"/>
<!-- 配置数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
<!-- 数据库驱动 -->
<property name="driverClassName" value="${jdbc.driver}" />
<!-- 连接字符串 -->
<property name="url" value="${jdbc.url}" />
<!-- 数据库用户名和密码 -->
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="maxTotal" value="${jdbc.maxTotal}" />
<property name="maxIdle" value="${jdbc.maxIdle}" />
<property name="initialSize" value="${jdbc.initialSize}" />
</bean>
<!-- 声明事务管理器,依赖于数据源 -->
<bean
id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 基于annotation(注解)的声明式事务 -->
<tx:annotation-driven transaction-manager="transactionManager" />
<!-- 配置mybatis工厂 -->
<bean id="sqlSessionFactory"
class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 注入数据源 -->
<property name="dataSource" ref="dataSource" />
<!-- 加载mybatis配置文件 -->
<property name="configLocation"
value="classpath:mybatis-config.xml" />
</bean>
<!-- 传统Dao模式开发-->
<bean id="customerDao" class="com.itheima.dao.impl.CustomerDaoImpl">
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>
<bean id="customerMapper"
class="org.mybatis.spring.mapper.MapperFactoryBean">
<!-- value=包+接口 -->
<property name="mapperInterface"
value="com.mapper.CustomerMapper"/>
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>
<!-- mapper代理开发(基于MapperScannerConfigurer)-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 加载包;value=包名(不要写到类);不必引用sqlSessionFactory-->
<property name="basePackage" value="com.mapper" />
</bean>
</beans>
mapper(MFB和MSC)代理开发注意事项及遵循的规范:
1、mapper接口的名称要与mapper映射的文件一致
2、Mapper.xml 文件中的 namespace与Mapper 接口的类路径相同(即接口文件和映射文件需要放在同一个包 中)
若上述两条遵循,那么可以不用在mybatis-config中引入mapper.xml
3、Mapper接口中的方法名要与mapper.xml中定义的每条执行语句一致
4、参数要和parameterType一致
5、返回类型要和resultType一致
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/emis
jdbc.username=root
jdbc.password=root
jdbc.maxTotal=30
jdbc.maxIdle=10
jdbc.initialSize=5
mybatis-conlig.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>
<mappers>
<mapper resource="mapper/Customer.xml"/> <!-- 加载映射文件 -->
</mappers>
</configuration>
映射文件Customer.xml
<?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="mc">
<select id="findID" parameterType="Integer"
resultType="com.itheima.po.Customer">
select * from customer where id=#{id}
</select>
</mapper>
实体类
属性名要与数据库中的属性名一致
package com.itheima.po;
public class Customer {
private int id;
private String name;
private String jobs;
private String phone;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getJobs() {
return jobs;
}
public void setJobs(String jobs) {
this.jobs = jobs;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String toString()
{
return id+","+name+","+jobs+","+phone+"\n";
}
}
//Dao接口
package com.itheima.dao;
import com.itheima.po.Customer;
public interface CustomerDao {
public Customer findID(Integer id);
}
//Dao实现类
package com.itheima.dao.impl;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import com.itheima.dao.CustomerDao;
import com.itheima.po.Customer;
public class CustomerDaoImpl extends SqlSessionDaoSupport
implements CustomerDao{
@Override
public Customer findID(Integer id) {
return
this.getSqlSession().selectOne
("mc.findID", id); //命名空间+SQL执行语句的id
}
}
测试类
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.itheima.dao.CustomerDao;
import com.itheima.po.Customer;
import com.mapper.CustomerMapper;
public class Test1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationContext applicationContext = new
ClassPathXmlApplicationContext("beans.xml");
//传统的Dao模式
//根据容器中的id获取bean
// CustomerDao cd = (CustomerDao) applicationContext.getBean("customerDao");
//
// Customer c = cd.findID(1);
// mapper接口开发(MapperFactoryBean和MapperScannerConfigurer类)
// 通过className.class获取bean
CustomerMapper cm = applicationContext.getBean(CustomerMapper.class);
Customer c = cm.findId(1);
System.out.println(c);
}
}