目录
(1)Spring默认的数据源DriverManagerrDataSource
(3)C3P0数据源ComboPooledDataSource
在前面的博客当中全都是一些基本的概念以及简单的实现原理,并没有讲解IOC的实际应用,其实IOC和AOP主要有两个方面的应用。一个是JDBC,另外一个是事务。这一篇主要是讲解JDBC,也就是IOC的应用。
之前我们对数据库进行操作时候都是把全部的配置文件全部写在了Mybatis.xml中,在这里我们在spring的配置文件spring-dao中来声明。下面通过代码直接看思路。
一、导入jar
二、搭建测试环境
先看一下项目结构:
1、定义Student
public class Student {
private Integer id;
private String name;
private int age;
//无参构造器和有参构造器
//get和set方法
//toString方法
}
2、定义数据库
3、定义接口
也就是对数据库增删改查的操作
public interface IStudentDao {
void insertStudent(Student student);
void deleteById(int id);
void updateStudent(Student student);
List<Student> selectAllStudents();
Student selectStudentById(int id);
}
4、定义mapper
<?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="com.fdd.dao.IStudentDao">
<insert id="insertStudent">
insert into student(name,age) values(#{name}, #{age})
</insert>
<delete id="deleteById">
delete from student where id=#{xxx}
</delete>
<update id="updateStudent">
update student set name=#{name}, age=#{age} where id=#{id}
</update>
<select id="selectAllStudents" resultType="Student">
select id,name,age from student
</select>
<select id="selectStudentById" resultType="Student">
select id,name,age from student where id=#{xxx}
</select>
</mapper>
注意:
- 返回值类型直接为Student,这是因为我们在mybatis.xml文件中已经配置,
- insert的id要与接口中的插入函数名一致
5、定义mybatis.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>
<package name="com.fdd.beans"/>
</typeAliases>
<mappers>
<package name="com.fdd.dao"/>
</mappers>
</configuration>
6、service层
首先是接口
public interface IStudentService {
void addStudent(Student student);
void removeById(int id);
void modifityStudent(Student student);
List<String> findStudentsByNames();
String fingStudentNameById(int id);
List<Student> findAllStudents();
Student findStudentById(int id);
}
然后是接口的实现:
package com.fdd.service;
import java.util.ArrayList;
import java.util.List;
import com.fdd.beans.Student;
import com.fdd.dao.IStudentDao;
public class StudentServiceImpl implements IStudentService {
IStudentDao dao;
public void setDao(IStudentDao dao) {
this.dao = dao;
}
public void addStudent(Student student) {
dao.insertStudent(student);
}
public void removeById(int id) {
dao.deleteById(id);
}
public void modifityStudent(Student student) {
dao.updateStudent(student);
}
public List<String> findStudentsByNames() {
List<String> names=new ArrayList<String>();
List<Student> students=this.findAllStudents();
for (Student student : students) {
names.add(student.getName());
}
return names;
}
public String fingStudentNameById(int id) {
Student student=this.findStudentById(id);
return student.getName();
}
public List<Student> findAllStudents() {
return dao.selectAllStudents();
}
public Student findStudentById(int id) {
return dao.selectStudentById(id);
}
}
7、spring-dao.xml文件的编写
首先考虑一下思路,我们之前在写mybatis.xml的时候,需要配置数据源,在这里我们依然要配置数据源
第一步:引入jdbc.properties
由于把数据库相关的配置放在了jdbc.properties中。需要先引入进来。
jdbc.properties配置文件如下:
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/test1
jdbc.user=root
jdbc.password=root
在spring-dao中声明:
<!-- 注册属性文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
当然还有另外一种方式(不常用):
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:jdbc.properties"/>
</bean>
第二步:接下来配置数据源,
这里有三种配置数据源的方式:
(1)Spring默认的数据源DriverManagerrDataSource
<!-- 默认的数据源 -->
<bean id="myDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClass" value="${jdbc.driver}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.user}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
(2)DBCP数据源BasicDataSource
使用这个数据源之前,还需要导入两个jar包。
<!-- 配置DBCP数据源 -->
<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClass" value="${jdbc.driver}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.user}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
(3)C3P0数据源ComboPooledDataSource
<!-- 注册数据源:C3P0 -->
<bean id="myDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.user}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
第三步:配置SQL工厂
<bean id="mySqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="classpath:mybatis.xml"/>
<property name="dataSource" ref="myDataSource"/>
</bean>
我们把数据源和配置文件加载进来。
第四步:生成Dao的代理对象
<!-- 生成Dao的代理对象 -->
<bean id="studentDao" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="sqlSessionFactory" ref="mySqlSessionFactory"/>
<property name="mapperInterface" value="com.fdd.dao.IStudentDao"/>
</bean>
第五步:配置service
<!-- 注册Service -->
<bean id="studentService" class="com.fdd.service.StudentServiceImpl">
<property name="dao" ref="studentDao"/>
</bean>
8、测试
public class MyTest {
private IStudentService service;
@Before
public void before(){
String resource = "applicationContext.xml";
ApplicationContext ac = new ClassPathXmlApplicationContext(resource);
service =(IStudentService) ac.getBean("studentService");
}
@Test
public void test01(){
Student student = new Student("wangwu",29);
service.addStudent(student);
}
@Test
public void test02(){
service.removeById(38);
}
@Test
public void test03(){
Student student = new Student("fdd",39);
student.setId(39);
service.modifityStudent(student);
}
@Test
public void test04(){
List<String> names = service.findStudentsByNames();
for (String string : names) {
System.out.println(string);
}
}
@Test
public void test05(){
String name = service.fingStudentNameById(27);
System.out.println(name);
}
@Test
public void test06(){
List<Student> students=service.findAllStudents();
for (Student student : students) {
System.out.println(student);
}
}
@Test
public void test07(){
Student student =service.findStudentById(27);
System.out.println(student);
}
}