首页 > 代码库 > spring+mybatis结合实例

spring+mybatis结合实例

1.通过上两篇文章的学习,大致了解了spring和mybatis的架构和使用原理,下面这篇文章就将给出两者结合的一个小实例,通过该实例进一步探索这两个框架的魅力,工程所需要的所有jar包都在链接:http://pan.baidu.com/s/1kUZmpWb

2.spring+mybatis结合实例

  (1)建立数据库mybatis,建立一张user表:

  技术分享

  (2)建立User.java类:

  

package com.springMyBatis.system.model;

public class User {
    private int id;
    private String username;
    private String password;

    public User() {
    }

    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 String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String toString() {
        return "User[id=" + id + " , username=" + username + " , password=" + password + "]";
    }
}

  (3)建立UserDao数据访问接口:

  

package com.springMyBatis.system.dao;

import com.springMyBatis.system.model.User;

public interface UserDao {
    public User getUser(User user);

    public void addUser(User user);

    public void updateUser(User user);

    public void deleteUser(int UserId);
}

  (4)建立数据访问接口映射文件:

  

<?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.springMyBatis.system.dao.UserDao">
    <select id="getUser" parameterType="User" resultType="User">
        SELECT *
        FROM user WHERE username=#{username} AND password=#{password}
    </select>
    <insert id="addUser" parameterType="User" flushCache="true">
        INSERT INTO
        user (id,username,password) VALUES
        (#{id},#{username},#{password})
    </insert>
    <update id="updateUser" parameterType="User">
        UPDATE user SET
        password=#{password} WHERE id=#{id}
    </update>
    <delete id="deleteUser" parameterType="int">
        DELETE FROM user WHERE
        id=#{id}
    </delete>
</mapper>

  (5)建立mybatis配置文件:

  

<?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>
        <!--这里给实体类取别名,方便在mapper配置文件中使用 -->
        <typeAlias alias="User" type="com.springMyBatis.system.model.User" />
    </typeAliases>
    <mappers>
        <mapper resource="com/springMyBatis/system/dao/UserDao.xml" />
    </mappers>
</configuration> 

  (6)建立spring配置文件:

  

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:tx="http://www.springframework.org/schema/tx" 
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans   
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd   
     http://www.springframework.org/schema/tx   
     http://www.springframework.org/schema/tx/spring-tx-3.0.xsd   
    http://www.springframework.org/schema/aop    
     http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
    <!-- 配置数据源 -->
    <bean id="jdbcDataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName">
            <value>com.mysql.jdbc.Driver</value>
        </property>
        <property name="url">
            <value>jdbc:mysql://localhost:3306/mybatis?useUnicode=true&amp;characterEncoding=UTF-8
            </value>
        </property>
        <property name="username">
            <value>root</value>
        </property>
        <property name="password">
            <value>geningchao</value>
        </property>
    </bean>
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="jdbcDataSource" />
        <property name="configLocation" value="classpath:mybatis-config.xml"></property>
    </bean>
    <bean id="userDao" class="org.mybatis.spring.mapper.MapperFactoryBean">
        <property name="mapperInterface" value="com.springMyBatis.system.dao.UserDao"></property>
        <property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
    </bean>
</beans> 

  (7)建立测试类:

  

package com.springMyBatis.system.test;

import java.util.Random;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.springMyBatis.system.dao.UserDao;
import com.springMyBatis.system.model.User;

public class UserController {
    public static void main(String[] args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("ApplicationContext.xml");
        UserDao userDao = (UserDao) ctx.getBean("userDao");
        User user = new User();
        // 添加两条数据
        user.setId(new Random().nextInt(10000));
        user.setUsername("Jessica");
        user.setPassword("123");
        userDao.addUser(user);
        user.setId(new Random().nextInt(10000));
        user.setUsername("Jessica2");
        user.setPassword("123");
        userDao.addUser(user);
        System.out.println("添加成功");
        // 查询数据
        user.setUsername("Jessica");
        user.setPassword("123");
        System.out.println(userDao.getUser(user).toString());
        user.setUsername("Jessica2");
        user.setPassword("123");
        System.out.println(userDao.getUser(user).toString());
        // 修改数据
        user.setId(new Random().nextInt(10000));
        user.setPassword("802");
        userDao.updateUser(user);
        System.out.println("修改成功");
        // 删除数据
        userDao.deleteUser(1);
        System.out.println("删除成功");

    }
}

3.工程分析

  该工程包括四部分,第一部分为基本的User实体类和UserDao访问接口,第二部分为mybatis接管的数据访问接口映射文件和mybatis配置文件,接口映射文件实现所有的数据访问控制,配置文件加载映射文件;第三部分为spring配置文件,该文件包含了连接数据库,加载mybatis配置文件;第四部分就是测试类了,测试类很容易懂;整体流程就是这样,希望通过这篇文章理清spring+mybatis的流程.

 

  以上仅是个人学习观点,若有不确之处,还请指点扶正!

spring+mybatis结合实例