美文网首页
Spring + Mybatis 基本用法

Spring + Mybatis 基本用法

作者: shpunishment | 来源:发表于2018-08-23 19:51 被阅读0次

Spring和Mybatis需要mybatis-spring-1.3.2.jar

Customer

package com.shpun.entity;
public class Customer {
    private String id;
    private String name;
    private String gender;
    private String birthday;
    private String tel;
    private String email;
    private String introduction;
    /*getter setter*/
}

CustomerMapper

package com.shpun.mapper;
@Repository  // 用于Spring扫描Mapper
public interface CustomerMapper {
    int insertCustomer(Customer customer);
    int deleteCustomer(String id);
    int updateCustomer(Customer customer);
    Customer selectCustomer(String id);
    List<Customer> selectAllCustomers();
}

CustomerMapper.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.shpun.mapper.CustomerMapper">

    <insert id="insertCustomer" useGeneratedKeys="true" keyProperty="id" parameterType="customer">
        insert into customers (name,gender,birthday,tel,email,introduction) value (#{name},#{gender},#{birthday},#{tel},#{email},#{introduction})
    </insert>

    <delete id="deleteCustomer" parameterType="string">
        delete from customers where id=#{id}
    </delete>

    <update id="updateCustomer" parameterType="customer">
        update customers set name=#{name},gender=#{gender},birthday=#{birthday},tel=#{tel},email=#{email},introduction=#{introduction} where id=#{id}
    </update>

    <select id="selectCustomer" parameterType="string" resultType="customer">
        select * from customers where id=#{id}
    </select>

    <select id="selectAllCustomers" resultType="customer">
        select * from customers
    </select>

</mapper>

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>

    <settings>
        <!-- 全局映射器启用缓存 -->
        <setting name="cacheEnabled" value="true"/>
        <!-- 允许JDBC支持生成的键 -->
        <setting name="useGeneratedKeys" value="true"/>
        <!-- 配置默认执行器 Reuse 重用预处理语句 -->
        <setting name="defaultExecutorType" value="REUSE"/>
        <!-- 全局启用延迟加载 -->
        <setting name="lazyLoadingEnabled" value="true"/>
        <!-- 设置超时时间,决定驱动等待一个数据库相应的时间 -->
        <setting name="defaultStatementTimeout" value="2500"/>
    </settings>

    <!-- 别名 -->
    <typeAliases>
        <typeAlias alias="customer" type="com.shpun.entity.Customer"/>
    </typeAliases>

    <!-- 映射器路径 -->
    <mappers>
        <mapper resource="com/shpun/mapper/CustomerMapper.xml"/>
    </mappers>

</configuration>

spring-config.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: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">

    <!-- 引入外部properties文件 -->
    <context:property-placeholder location="mysql.properties"/>

    <!-- 设置数据库信息等 -->
    <bean id="dateSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
        <property name="driverClassName" value="${jdbc.driver}"/>
        <property name="url" value="${jdbc.url}"/>
    </bean>

    <!-- SqlSessionFactoryBean 去支持 SqlSessionFactory 配置 -->
    <bean  id="SqlSessionFactory"  class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dateSource"/>
        <property name="configLocation" value="mybatis-config.xml"/>
    </bean>

    <!-- 自动扫描配置Mapper的类 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!-- 指定Spring扫描的包 -->
        <property name="basePackage" value="com.shpun.mapper"/>
        <!-- 指定Spring中SqlSessionFactory的Bean的名称 -->
        <property name="SqlSessionFactoryBeanName" value="SqlSessionFactory"/>
        <!-- 指定类被注解标识才扫描 @Repository -->
        <property name="annotationClass" value="org.springframework.stereotype.Repository"/>
    </bean>

</beans>

使用log4j需要配置log4j.properties

log4j.rootLogger=DEBUG,stdout
log4j.logger.org.mybatis=DEBUG
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p %d %c: %m%n

测试

package com.shpun.main;
public class TestMain {
    public static void main(String[] args){

        Logger logger = Logger.getLogger(TestMain.class);
        ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
        CustomerMapper customerMapper = context.getBean(CustomerMapper.class);

        Customer customer = new Customer();
        customer.setName("test");
        customer.setIntroduction("test Spr Myb");

        customerMapper.insertCustomer(customer);
        logger.info("insert id : "+customer.getId());

        customerMapper.deleteCustomer("1028");

        customer.setId("1029");
        customer.setIntroduction("update 1029 修改成功");
        customerMapper.updateCustomer(customer);

        logger.info(customerMapper.selectCustomer("1029"));

        logger.info(customerMapper.selectAllCustomers());
    }
}

相关文章

网友评论

      本文标题:Spring + Mybatis 基本用法

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