Main
package test; import entity.Student; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import service.IStudentService; public class Main { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); IStudentService studentService = (IStudentService)context.getBean("studentService"); Student student = new Student(); student.setStuNo(6); student.setStuName("666"); student.setStuAge(66); studentService.addStudent(student); System.out.println("ok"); } }
Student
package entity; public class Student { private int stuNo; private String stuName; private int stuAge; public int getStuNo() { return stuNo; } public void setStuNo(int stuNo) { this.stuNo = stuNo; } public String getStuName() { return stuName; } public void setStuName(String stuName) { this.stuName = stuName; } public int getStuAge() { return stuAge; } public void setStuAge(int stuAge) { this.stuAge = stuAge; } }View Code
IStudentService
package service; import entity.Student; public interface IStudentService { public void addStudent(Student student); }View Code
StudentServiceImpl
package service.impl; import entity.Student; import mapper.StudentMapper; import service.IStudentService; public class StudentServiceImpl implements IStudentService { public void setStudentMapper(StudentMapper studentMapper) { this.studentMapper = studentMapper; } private StudentMapper studentMapper; @Override public void addStudent(Student student) { studentMapper.addStudent(student); } }View Code
StudentDaoImpl
package dao.impl; import entity.Student; import mapper.StudentMapper; import org.apache.ibatis.session.SqlSession; import org.mybatis.spring.support.SqlSessionDaoSupport; public class StudentDaoImpl extends SqlSessionDaoSupport implements StudentMapper { @Override public void addStudent(Student student) { SqlSession session = super.getSqlSession() ; StudentMapper stuDao = session.getMapper(StudentMapper.class) ; stuDao.addStudent(student); } }View Code
StudentMapper
package mapper; import entity.Student; public interface StudentMapper { public void addStudent(Student student); }View Code
StudentMapper.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"> <!-- namespace:该mapper.xml映射文件的 唯一标识 --> <mapper namespace="mapper.StudentMapper"> <select id="queryStudentByStuno" parameterType="int" resultType="entity.Student"> select * from student where stuno = #{stuNo} </select> <insert id="addStudent" parameterType="entity.Student"> insert into student(stuno,stuname,stuage) values(#{stuNo},#{stuName},#{stuAge}) </insert> </mapper>View Code
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="config" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer"> <property name="locations"> <array> <value>classpath:db.properties</value> </array> </property> </bean> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="${driver}"></property> <property name="url" value="${url}"></property> <property name="username" value="${username}"></property> <property name="password" value="${password}"></property> </bean> <bean id="sqlSessionFacotry" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="mapperLocations" value="mapper/*.xml"></property> </bean> <!-- 第一种方式生成mapper对象 --> <!--<bean id="studentMapper" class="dao.impl.StudentDaoImpl">--> <!--<property name="sqlSessionFactory" ref="sqlSessionFacotry"></property>--> <!--</bean>--> <!-- 第二种方式生成mapper对象 --> <!--<bean id="studentMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">--> <!--<property name="mapperInterface" value="mapper.StudentMapper"></property>--> <!--<property name="sqlSessionFactory" ref="sqlSessionFacotry"></property>--> <!--</bean>--> <!-- 第三种方式生成mapper对象(批量产生多个mapper) 批量产生Mapper对在SpringIOC中的 id值 默认就是 首字母小写接口名 (首字母小写的接口名=id值) --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="sqlSessionFactoryBeanName" value="sqlSessionFacotry"></property> <property name="basePackage" value="mapper"></property> </bean> <bean id="studentService" class="service.impl.StudentServiceImpl"> <property name="studentMapper" ref="studentMapper"></property> </bean> </beans>
db.properties
driver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/jdbc username=root password=root maxIdle=1000 maxActive=500