mybatis

作者: 北海北_6dc3 | 来源:发表于2019-10-27 11:50 被阅读0次

    参考资料
    mybatis
    spring-mybatis
    源码:git@code.aliyun.com:springmvc/simpleSpringMvc.git
    mybatis分支

    1、mybatis-spring引入

    根据spring版本如果5以下,我们只能选择1.3版本。

    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis-spring</artifactId>
        <version>1.3.2</version>
    </dependency>
    

    2、mysql-connector-java引入

    mysql-connector-java与mysql对应版本.png
    mysql-connector-java与java对应版本.png
    参考资料:
    https://blog.csdn.net/lingeio/article/details/93487770
    https://dev.mysql.com/doc/connector-j/5.1/en/connector-j-versions.html

    整体pom依赖如下:

                <dependency>
                    <groupId>mysql</groupId>
                    <artifactId>mysql-connector-java</artifactId>
                    <version>8.0.17</version>
                </dependency>
                <dependency>
                    <groupId>org.mybatis</groupId>
                    <artifactId>mybatis-spring</artifactId>
                    <version>1.3.0</version>
                </dependency>
                <dependency>
                    <groupId>org.mybatis</groupId>
                    <artifactId>mybatis</artifactId>
                    <version>3.4.0</version>
                </dependency>
                <dependency>
                    <groupId>com.alibaba</groupId>
                    <artifactId>druid</artifactId>
                    <version>1.1.20</version>
                </dependency>
              <dependency>
                  <groupId>org.springframework</groupId>
                 <artifactId>spring-jdbc</artifactId>
              </dependency>
    

    Mapper建立:

    public interface UserMapper {
        User selectUser1(String username);
    }
    

    Entity建立

    @Data
    public class User {
        private int id;
        private int age;
        private String name;
        private int type;
    }
    

    Service层接口

    public interface UserService {
        User getUserByUsername(String username);
    }
    

    Service层实现

    //供mapper使用
    @Service("userService")
    public class UserServiceImpl implements UserService {
        @Autowired
        private UserMapper userMapper;
        @Override
        public User getUserByUsername(String username) {
            User user = userMapper.selectUser1(username);
            return user;
        }
    }
    
    //使用sqlSessionTemplate
    @Service("userService1")
    public class UserService1Imp implements UserService {
        @Autowired
        private SqlSessionTemplate sqlSessionTemplate;
    
        @Override
        public User getUserByUsername(String username) {
            User user =sqlSessionTemplate.selectOne("com.jc.service.mysql.mapper.UserMapper.selectUser1",username);
            return user;
        }
    }
    

    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.jc.service.mysql.mapper.UserMapper">
        <select id="selectUser1" parameterType="java.lang.String" resultType="com.jc.service.mysql.entity.User">
            select * from user where name=#{username}
        </select>
    </mapper>
    

    mybatis配置:

    <!--使用MapperScannerConfigurer-->
    <?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">
    
    
        <context:property-placeholder location="classpath:config/jdbc.properties"/>
        <!-- 引入配置文件 -->
        <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close" init-method="init">
            <property name="driverClassName" value="${jdbc_driverClassName}"/>
            <property name="url" value="${jdbc_url}"/>
            <property name="username" value="${jdbc_username}"/>
            <property name="password" value="${jdbc_password}"/>
            <!-- 初始化连接大小 -->
            <property name="initialSize" value="${initialSize}"/>
            <!-- 连接池最大使用连接数量 -->
            <property name="maxActive" value="${maxActive}"/>
            <!-- 连接池最小空闲 -->
            <property name="minIdle" value="${minIdle}"/>
            <!-- 获取连接最大等待时间 -->
            <property name="maxWait" value="${maxWait}"/>
            <property name="validationQuery" value="${validationQuery}"/>
            <property name="testOnBorrow" value="false"/>
            <property name="testOnReturn" value="false"/>
            <property name="testWhileIdle" value="true"/>
            <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
            <property name="timeBetweenEvictionRunsMillis" value="10000"/>
            <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
            <property name="minEvictableIdleTimeMillis" value="25200000"/>
            <!-- 打开removeAbandoned功能 -->
            <property name="removeAbandoned" value="true"/>
            <!-- 1800秒,也就是30分钟 -->
            <property name="removeAbandonedTimeout" value="1800"/>
            <!-- 关闭abanded连接时输出错误日志 -->
            <property name="logAbandoned" value="true"/>
            <!-- 监控数据库 -->
            <!-- <property name="filters" value="stat" /> -->
            <property name="filters" value="mergeStat"/>
        </bean>
    
    
        <!-- myBatis文件 -->
        <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            <property name="dataSource" ref="dataSource"/>
            <!-- 配置数据库表对应的java实体类,如果是非完全限定名,如返回参数User,而不是写成com.jc.service.mysql.entity.User,
             如果是完全限定,可省略-->
            <property name="typeAliasesPackage" value="com.jc.service.mysql.entity"/>
            <!-- 自动扫描entity目录, 省掉Configuration.xml里的手工配置 -->
            <property name="mapperLocations" value="classpath:mapper/*.xml"/>
        </bean>
    
        <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            <property name="basePackage" value="com.jc.service.mysql.mapper"/>
            <!-- 如果仅有一个数据源,可省略 -->
            <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
        </bean>
    </beans>
    
    <!--使用SqlSessionTemplate,仅更改扫描-->
        <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
            <constructor-arg ref="sqlSessionFactory"/>
        </bean>
    

    相关文章

      网友评论

          本文标题:mybatis

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