美文网首页
spring-mybatis整合

spring-mybatis整合

作者: 我只是旁白 | 来源:发表于2017-11-08 20:02 被阅读0次

    spring-mybatis

    2、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"
       xmlns:aop="http://www.springframework.org/schema/aop"
       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/aop 
       http://www.springframework.org/schema/aop/spring-aop.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd">
    
    <!--开启组建扫描-->
    <context:component-scan base-package="service所在的包"/>
    
    <!--加载properties配置文件-->
    <context:property-placeholder location="数据库配置文件"/>
    
    <bean class="com.alibaba.druid.pool.DruidDataSource" id="dataSource">
        <property name="driverClassName" value="${jdbc.driverClassName}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>
    
    <!--配置SqlSessionFactory-->
    <bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sessionFactory">
        <!--数据源-->
        <property name="dataSource" ref="dataSource"/>
        <!--扫描mapper文件-->
        <property name="mapperLocations" value="mapper xml文件所在目录"/>
        <!--配置别名,可有可无-->
        <property name="typeAliasesPackage" value="实体类所在目录"/>
        <!--加载配置文件,可有可无-->
        <!--<property name="configLocation" value="mybatis配置文件"/>-->
    </bean>
    
    <!--配置MapperScannerConfigurer:用来扫描mapper接口,并且创建接口的实现类 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer" id="mapperScannerConfigurer">
        <!--注入sqlSessionFactory-->
        <property name="sqlSessionFactoryBeanName" value="sessionFactory"/>
        <!--扫描某个包下的接口-->
        <property name="basePackage" value="实体类包名"/>
    </bean>
    

    db.properties

    jdbc.driverClassName=com.mysql.jdbc.Driver

    jdbc.url=jdbc:mysql:///taobao?useUnicode=true&characterEncoding=utf-8

    jdbc.username=root

    jdbc.password=

    3、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.study.dao.UserDao">
    <select id="selectUserById" resultType="User">
        select * from user where uid=#{value}
    </select>
    </mapper>
    

    4、dao层

    public interface UserDao {
        public User selectUserById(Integer uid);
    }
    

    5、service层

    @Service //创建对象
    public class UserServiceImpl implements IUserService {
        @Autowired //注入userdao的实现类
        private UserDao userDao;
    
        public User selectUserById(Integer uid) {
            return userDao.selectUserById(uid);
        }
    }
    

    6、测试类

    public void testSelectUserById(){
        ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("spring-mybatis.xml");
        IUserService service = context.getBean(UserServiceImpl.class);
        User user = service.selectUserById(3);
        System.out.println(user);
    }
    

    1、导包

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <!--下面是spring的jar-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.8.10</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!--mybatis-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.5</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.3.1</version>
        </dependency>
    
        <!--数据源-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.4</version>
        </dependency>
    
        <!--mysql-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.30</version>
        </dependency>
      </dependencies>
    

    相关文章

      网友评论

          本文标题:spring-mybatis整合

          本文链接:https://www.haomeiwen.com/subject/rmzkuxtx.html