MyBatis框架学习完结篇

Spring与MyBatis整合

jar包

jar

配置文件

applicationContext.xml

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
		http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
		http://www.springframework.org/schema/mvc
		http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
		http://www.springframework.org/schema/context
		http://www.springframework.org/schema/context/spring-context-3.2.xsd
		http://www.springframework.org/schema/aop
		http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
		http://www.springframework.org/schema/tx
		http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">


    <!-- 加载java的配置文件 -->
    <context:property-placeholder location="db.properties" />

    <!-- 创建数据源 -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="${db.driver}" />
        <property name="url" value="${db.url}" />
        <property name="username" value="${db.username}" />
        <property name="password" value="${db.password}" />
        <property name="maxActive" value="10" />
        <property name="maxIdle" value="5" />
    </bean>

    <!-- SqlSessionFactory -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 指定mybatis的全局配置文件的路径 -->
        <!--<property name="configLocation" value="mybatis/SqlMapConfig.xml"></property>-->
        <!-- 数据源 -->
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!-- 配置UserMapper代理类 单个配置文件-->
    <!--<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">-->
        <!--&lt;!&ndash; 设置代理类的接口 &ndash;&gt;-->
        <!--<property name="mapperInterface" value="dao.mapper.UserMapper"></property>-->
        <!--&lt;!&ndash; 依赖注入SqlSessionFactory &ndash;&gt;-->
        <!--<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>-->
    <!--</bean>-->


    <!-- 批量配置mapper代理类,默认bean的id为类名首字母小写 多个mapper映射-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!-- 配置扫描的包 -->
        <property name="basePackage" value="dao.mapper"></property>

        <!-- 默认不需要配置SqlSessionFactory(只有一个SqlSessionFactory时),单独配置也可以 -->
        <!-- <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property> -->
    </bean>
</beans>

db.properties

db.driver = com.mysql.jdbc.Driver
db.url = jdbc:mysql://localhost:3306/ee19_batis_day03?useUnicode=true&characterEncoding=utf-8
db.username = root
db.password = 123

log4j.properties

# Global logging configuration
log4j.rootLogger=DEBUG, stdout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

Mapper

UserMapper

package dao.mapper;

import entity.User;

public interface UserMapper {
    public User findUserById(int id);
}

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="dao.mapper.UserMapper">

    <select id="findUserById" parameterType="int" resultType="entity.User">
      SELECT * FROM USER WHERE id = #{id}
    </select>

</mapper>

实体类

package entity;

import java.util.Date;

public class User {
    private int id;
    private String username;
    private Date birthday;
    private String address;
    private String sex;

    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 Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", birthday=" + birthday +
                ", address='" + address + '\'' +
                ", sex='" + sex + '\'' +
                '}';
    }
}

测试类

public class MyBatis {
    ApplicationContext ctx;
    @Test
    public void testFindOrderAndUser() throws Exception {
        ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserMapper userMapper = (UserMapper) ctx.getBean("userMapper");
        User user = userMapper.findUserById(1);
        System.out.println("user = " + user);
    }
}

测试结果

在这里插入图片描述

版权声明:本文为qq_29951983原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_29951983/article/details/87536736