mybatis学习笔记 spring与mybatis整合
转载自http://blog.csdn.net/naruto_Mr/article/details/48239357
1.创建web工程,导入spring依赖包与mybatis依赖包,还需要mybatis-spring.jar,这是spring连接mybatis的桥梁.下载地址:http://mvnrepository.com/artifact/org.mybatis/mybatis-spring
2.配置文件
1)web.xml配置,指定spring配置文件名为config/spring-cfg.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app version="2.4"
- xmlns="http://java.sun.com/xml/ns/j2ee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
- http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
- <!-- spring入口 -->
- <!-- 自动加载spring配置文件WEB-INF/applicationContenxt.xml -->
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
- <!-- 修改spring配置文件路径 -->
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>WEB-INF/classes/config/spring-cfg.xml</param-value>
- </context-param>
- </web-app>
2)spring-cfg.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: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-3.2.xsd
- http://www.springframework.org/schema/tx
- http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-3.2.xsd">
- <import resource="classpath:config/spring-mybatis.xml"/>
- <!-- 开启注解注入 -->
- <context:annotation-config></context:annotation-config>
- <context:component-scan base-package="com.skymr.mybatis.dao"></context:component-scan>
- <context:component-scan base-package="com.skymr.mybatis.service"></context:component-scan>
- </beans>
引用mybatis的spring配置文件,开启自动扫描功能,扫描dao层与service层.
3)spring-mybatis.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-2.0.xsd">
- <!-- 数据源 -->
- <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
- <!-- 这个属性driverClassName为什么在DriverManagerDataSource及父类中找不到呢 -->
- <property name="driverClassName" value="com.mysql.jdbc.Driver" />
- <property name="url" value="jdbc:mysql://127.0.0.1:3306/website"/>
- <property name="username" value="root" />
- <property name="password" value="root" />
- </bean>
- <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
- <property name="basePackage" value="com.skymr.mybatis.dao"></property>
- <property name="sqlSessionFactoryBeanName" value="sessionFactory"></property>
- </bean>
- <!-- sessionFactory配置 -->
- <bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
- <property name="dataSource" ref="dataSource" />
- <property name="mapperLocations" value="classpath:com/skymr/mybatis/mappers/*.xml"/>
- <property name="configLocation" value="classpath:config/mybatis-config.xml"/>
- </bean>
- </beans>
首先是配置数据源;
再配置了个MapperScannerConfigurer,这个是什么用的呢?
在Service的实现层中,要使用到dao层的实现层的实例,但是mybatis中是没有dao层实现层的啊,怎么注入到Service中呢,我想MapperScannerConfiger就是解决这个问题的,它是将mybatis自己对dao层的实例化后的实例注入到spring容器中,我们就可以取出后使用.
最后配置了SqlsessionFactory,指定mapper文件路径与mybatis的另一些配置.
3)mybatis-config.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.skymr.mybatis.model"/>
- </typeAliases>
- </configuration>
只是定义别名而已.
3.分层
1).Model层
- package com.skymr.mybatis.model;
- public class User {
- private int id;
- private String userName;
- private String password;
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public String getUserName() {
- return userName;
- }
- public void setUserName(String userName) {
- this.userName = userName;
- }
- public String getPassword() {
- return password;
- }
- public void setPassword(String password) {
- this.password = password;
- }
- public String toString(){
- return id+"," + userName+","+password;
- }
- }
2)Dao层
- package com.skymr.mybatis.dao;
- import com.skymr.mybatis.model.User;
- public interface UserDao {
- public User login(String userName, String password);
- }
3)mapper层,UserMapper.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.skymr.mybatis.dao.UserDao">
- <select id="login" resultType="User">
- select * from mybatis_user where userName=#{param1} and password=#{param2}
- </select>
- </mapper>
4).Service层
- package com.skymr.mybatis.service;
- import com.skymr.mybatis.model.User;
- public interface UserService {
- public User login(String userName, String password);
- }
- package com.skymr.mybatis.service.impl;
- import javax.annotation.Resource;
- import org.springframework.stereotype.Repository;
- import com.skymr.mybatis.dao.UserDao;
- import com.skymr.mybatis.model.User;
- import com.skymr.mybatis.service.UserService;
- @Repository("userService")
- public class UserServiceBean implements UserService{
- @Resource
- private UserDao userDao;
- public User login(String userName, String password) {
- return userDao.login(userName, password);
- }
- }
4.测试
- package com.skymr.mybatis.service;
- import org.junit.Test;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- import com.skymr.mybatis.model.User;
- public class LoginTest {
- @Test
- public void loginTest(){
- ApplicationContext context= new ClassPathXmlApplicationContext("/config/spring-cfg.xml");
- UserService service =(UserService) context.getBean("userService");
- User user = service.login("aaaa", "aaaa");
- System.out.println(user);
- }
- }