Spring整合mybatis完整示例

Spring整合mybatis完整示例

 

 

实现功能:根据id查找用户信息。

 

1、首先创建一个与表中数据相对应的实体类,User.java

package bean;

public class User {
    int id;
    String name;
    int age;
    String sex;
    String school;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getSex() {
        return sex;
    }

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

    public String getSchool() {
        return school;
    }

    public void setSchool(String school) {
        this.school = school;
    }

    @Override
    public String toString() {
        return ("姓名:"+name+"\n年龄:"+age+"\n性别"+sex+"\n学校"+school);
    }
}

 

2、写出这个类的映射接口,里面有我们要实现的查询的抽象方法。

package dao;

import bean.User;

public interface IUser {
    User getUserByID (int id);
}

 

3、写出这个类的映射Mapper文件,里面有select语句。

<?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.IUser">

    <select id="getUserByID" resultType="User" parameterType="int">
        SELECT * from user where id = #{id}
    </select>


</mapper>

 

4、以上都准备好之后,后面就是与没用spring不同的地方。

  • 准备mysql.properteis的参数配置文件,里面写上数据库连接要用到的参数。
  • jdbc.driver=com.mysql.jdbc.Driver
    jdbc.url=jdbc:mysql://localhost/test?useUnicode=true&characterEncoding=UTF-8
    jdbc.uid=root
    jdbc.password=123456


     

  • mybatis文件与之前不同,之前实在mybatis-config.xml中配置数据库连接的,现在要把这些放在spring的配置文件中,所以mybatis配置文件中只写类的别名和引用的Mapper

  • <?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>
            <typeAlias alias="User" type="bean.User"/>
        </typeAliases>
    
        <!--<environments default="development">-->
            <!--<environment id="development">-->
                <!--<transactionManager type="JDBC"/>-->
                <!--<dataSource type="POOLED">-->
                    <!--<property name="driver" value="com.mysql.jdbc.Driver"/>-->
                    <!--<property name="url" value="jdbc:mysql://127.0.0.1/test?useUnicode=true&amp;characterEncoding=UTF-8"/>-->
                    <!--<property name="username" value="root"/>-->
                    <!--<property name="password" value="123456"/>-->
                <!--</dataSource>-->
            <!--</environment>-->
        <!--</environments>-->
    
    
        <mappers>
            <!-- // power by http://www.yiibai.com -->
            <mapper resource="xml/User.xml"/>
        </mappers>
    
    
    
    </configuration>

     

  • spring-config.xml中,我们要配置数据库连接池,和sqlSessionFactory对象,以及UserMapper对象。

    • sqlSessionFactory中引用mybatis-config.xml文件

    • userMapper中标明要实现的接口

<?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:p="http://www.springframework.org/schema/p"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:tx="http://www.springframework.org/schema/tx"
        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-4.2.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">



<!--表明引用的参数配置文件是mysql-local.properties-->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>
                mysql-local.properties
            </value>
        </list>
    </property>
</bean>


    <!--数据库连接池-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driver}"/>
        <property name="jdbcUrl" value="${jdbc.url}"/>
        <property name="user" value="${jdbc.uid}"/>
        <property name="password" value="${jdbc.password}"/>
        <!-- 初始连接池大小 -->
        <property name="initialPoolSize" value="10"/>
        <!-- 连接池中连接最小个数 -->
        <property name="minPoolSize" value="5"/>
        <property name="maxPoolSize" value="20"/>
     </bean>


    <!-- 配置SqlSessionFactory对象 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 注入数据库连接池 -->
        <property name="dataSource" ref="dataSource"/>
        <property name="configLocation" value="mybatis-spring-config.xml"/>

    </bean>

    <!--<bean id="sqlsessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">-->
        <!--<constructor-arg index="0" ref="sqlSessionFactory" />-->
    <!--</bean>-->

    <!--配置userMapper对象-->
    <bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
        <property name="mapperInterface" value="dao.IUser"/>
        <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
    </bean>

</beans>

 

5、以上3个xml文件都配置完成之后,我们调用这个查询方法就只需要短短几行代码。

package bean;

import dao.IUser;
import org.apache.ibatis.session.SqlSession;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class DemoTest {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
        IUser userMapper = (IUser)context.getBean("userMapper");
        User user = userMapper.getUserByID(2);
        System.out.println(user);

    }

}

 

6、运行结果

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 书香水墨 设计师:CSDN官方博客 返回首页