定义学生类
/15-spring-mybatis2
package com.bjpowernode.beans;
public class Student {
private Integer id;
private String name;
private int age;
public Student() {
super();
// TODO Auto-generated constructor stub
}
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
public Integer getId() {
return id;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public void setId(Integer id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", age=" + age + "]";
}
}
定义Dao层接口
SQL语句执行的方法
package com.bjpowernode.dao;
import java.util.List;
import com.bjpowernode.beans.Student;
public interface IStudentDao {
void insertStudent(Student student);
void deleteById(int id);
void updateStudent(Student student);
List<Student> selectAllStudents();
Student selectStudentById(int id);
}
定义Dao层映射文件
注意这里映射文件与Dao层接口同名,后面会说明原因
这里写具体SQL语句实现,id与Dao层接口中方法名相同
insert into student(name,age) values(#{name}, #{age})
<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>
## Service层接口
package com.bjpwernode.service;
import java.util.List;
import com.bjpowernode.beans.Student;
public interface IStudentService {
void addStudent(Student student);
void removeById(int id);
void modifyStudent(Student student);
List<String> findAllStudentsNames();
String findStudentNameById(int id);
List<Student> findAllStudents();
Student findStudentById(int id);
}
Service层实现
实际上我们在这个类中创建了一个Dao层接口对象,这里方法的返回值就是Dao层方法的返回值。
package com.bjpwernode.service;
import java.util.ArrayList;
import java.util.List;
import com.bjpowernode.beans.Student;
import com.bjpowernode.dao.IStudentDao;
public class StudentServiceImpl implements IStudentService {
private IStudentDao dao;
public void setDao(IStudentDao dao) {
this.dao = dao;
}
@Override
public void addStudent(Student student) {
dao.insertStudent(student);
}
@Override
public void removeById(int id) {
dao.deleteById(id);
}
@Override
public void modifyStudent(Student student) {
dao.updateStudent(student);
}
@Override
public List<String> findAllStudentsNames() {
List<String> names = new ArrayList<String>();
List<Student> students = this.findAllStudents();
for(Student student : students) {
names.add(student.getName());
}
return names;
}
@Override
public String findStudentNameById(int id) {
Student student = this.findStudentById(id);
return student.getName();
}
@Override
public List<Student> findAllStudents() {
return dao.selectAllStudents();
}
@Override
public Student findStudentById(int id) {
return dao.selectStudentById(id);
}
}
mybatis主配置文件
与以往不同,这里只是指定实体类全限定性类名的别名,和注册IStudentDao映射文件。其他的注册DB四要素、运行环境等在applicationContext.xml配置
这里包名指定为dao,就不用配mapper.xml了,但需要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.bjpowernode.beans"/>
</typeAliases>
<mappers>
<!-- <mapper resource="com/bjpowernode/dao/mapper.xml"/> -->
<!-- 报名指定为dao,就不用配mapper.xml了,但需要xml与接口同名 -->
<package name="com.bjpowernode.dao"/>
</mappers>
</configuration>
applicationContext.xml配置文件
完成工作:
注册数据源c3p0、注册属性文件jdbc、使用支持扫面的Mapper动态代理生成Dao的代理对象、注册Service。
当前配置会为指定的基本包中所有的接口生成代理对象
这里的Dao的注入需要使用ref属性, 若Dao的接口名的前两个字母是大写, 则这里的值为接口的简单类名 ‘若只有首字母大写,第二个字母为小写,则这里应该为studentDao(接口的简单类名 首字母小写)
生成Dao的代理对象,也是为了获取SqlSession
<?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" xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 注册数据源: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>
<!-- 注册属性文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 配置映射文件和数据源 -->
<bean id="mySqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="classpath:mybatis.xml"/>
<property name="dataSource" ref="myDataSource"/>
</bean>
<!-- 生成Dao的代理对象
当前配置会为指定的基本包中所有的接口生成代理对象
-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="mySqlSessionFactory"/>
<property name="basePackage" value="com.bjpowernode.dao"/>
</bean>
<!-- 注册Service -->
<bean id="studentService" class="com.bjpwernode.service.StudentServiceImpl">
<!-- 这里的Dao的注入需要使用ref属性,
若Dao的接口名的前两个字母是大写, 则这里的值为接口的简单类名
若只有首字母大写,第二个字母为小写,则这里应该为studentDao(接口的简单类名 首字母小写)-->
<property name="dao" ref="IStudentDao"/>
</bean>
</beans>
也可以使用Mapper动态代理的方式生成Dao对象
<!-- 生成Dao的代理对象 -->
<bean id="studentDao" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="sqlSessionFactory" ref="mySqlSessionFactory"/>
<property name="mapperInterface" value="com.bjpowernode.dao.IStudentDao"/>
</bean>