Spring与MyBatis整合
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring与MyBatis整合相关的知识,希望对你有一定的参考价值。
1.导入jar包
aspectjweaver.jar
com.springsource.org.aopalliance-1.0.0.jar
commons-dbcp.jar
commons-logging.jar
commons-pool.jar
mybatis-3.2.8.jar
mybatis-spring-1.2.1.jar
mysql-connector-java-5.1.24-bin.jar
spring-aop-4.0.3.RELEASE.jar
spring-aspects-4.0.3.RELEASE.jar
spring-beans-4.0.3.RELEASE.jar
spring-context-4.0.3.RELEASE.jar
spring-core-4.0.3.RELEASE.jar
spring-expression-4.0.3.RELEASE.jar
spring-jdbc-4.0.3.RELEASE.jar
spring-tx-4.0.3.RELEASE.jar
spring-web-4.0.3.RELEASE.jar
2.分包
com.gxa.spring.controller
com.gxa.spring.dao
com.gxa.spring.service
com.gxa.spring.config
com.gxa.spring.entity
com.gxa.spring.service.impl
3.(entity)创建数据库(test/student)和与数据库对应的实体类(Student.class)
public class Student { private int id ; private String sname ; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getSname() { return sname; } public void setSname(String sname) { this.sname = sname; } }
4.(service)创建DAO层的接口(StudentDao)
public interface StudentDao { public List<Student> getStudent(); }
5.(entity)创建接口的映射文件(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"> <mapper namespace="com.gxa.spring.dao.StudentDao"> <select id="getStudent" resultType="com.gxa.spring.entity.Student"> select * from student </select> </mapper>
6.(config)让映射文件能在mybatis的配置文件中找到(mybatis-config.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="com.gxa.spring.dao.StudentDao"> <select id="getStudent" resultType="com.gxa.spring.entity.Student"> select * from student </select> </mapper>
7.(config)还差dao-spring整合的配置文件(jdbc.properties & spring-dao.xml)
jdbc.properties
mysql.driver=com.mysql.jdbc.Driver
mysql.url=jdbc:mysql://localhost:3306/test
mysql.username=root
mysql.password=xxxx
spring-dao.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" xmlns:p="http://www.springframework.org/schema/p" 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"> <!--读取数据源配置文件--> <context:property-placeholder location="classpath:com/gxa/spring/config/jdbc.properties"/> <!--定义数据源配置文件--> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${mysql.driver}"></property> <property name="url" value="${mysql.url}"></property> <property name="username" value="${mysql.username}"></property> <property name="password" value="${mysql.password}"></property> </bean> <!-- 利用Spring框架来创建和管理MyBatis的SqlSessionFactory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="configLocation" value="classpath:com/gxa/spring/config/mybatis-config.xml"></property> </bean> <!-- 扫描MyBatis Dao层中接口,同时关联接口中的方法和映射文件的标签id名称 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.gxa.spring.dao"></property> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property> </bean> </beans>
8.(service)创建service接口(StudentService)
public interface StudentService { public void getStudent(); }
9.(service.impl)创建service实现类(StudentServiceImpl.class)
@Service public class StudentServiceImpl implements StudentService { @Autowired private StudentDao studentDao; @Override public void getStudent() { List<Student> list = studentDao.getStudent(); for (Student s : list) { System.out.println(s.getId()+" "+s.getSname()); } } }
10.(config)创建配置文件来扫描service(spring-service.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" xmlns:p="http://www.springframework.org/schema/p" 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"> <context:component-scan base-package="com.gxa.spring.service"></context:component-scan> </beans>
11.(controller)编写controller(StudentServlet.java)
@Controller @WebServlet("/Student.do") public class StudentServlet extends HttpServlet { private static final long serialVersionUID = 1L; @Autowired private StudentService studentService; @Override public void init() throws ServletException { //获取上下文 WebApplicationContext webApplicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext()); AutowireCapableBeanFactory autowireCapableBeanFactory = webApplicationContext.getAutowireCapableBeanFactory(); autowireCapableBeanFactory.autowireBean(this); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { studentService.getStudent(); } }
12.(config)编写controller配置文件扫描controller(spring-controller.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" xmlns:p="http://www.springframework.org/schema/p" 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"> <context:component-scan base-package="com.gxa.spring.controller"></context:component-scan> </beans>
13.(WebContent)编写web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>spring</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <!--加载全部配置文件--> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:com/gxa/spring/config/spring-*.xml</param-value> </context-param> <!--编写listener--> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> </web-app>
以上是关于Spring与MyBatis整合的主要内容,如果未能解决你的问题,请参考以下文章