• Mybatis3.2和Spring3.x整合----Myb…


    1. 工程主要文件

    Mybatis3.2和Spring3.x整合----Mybatis+0

     

    2. 主要jar包

    mybatis-3.2.0.jar

    mybatis-spring-1.1.1.jar

    mysql-connector-java-3.1.13-bin.jar

    log4j-1.2.11.jar

    3. 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>

    <properties resource="resources/jdbc.properties"></properties>

    <typeAliases>

    <!-- 导入此包下的所有类,相当于注册。。 -->

    <package name="com.edu.mybatis_spring.model" />

    </typeAliases>

     

    <environments default="development">

    <environment id="development">

    <!-- 使用JDBC的事务管理 -->

    <transactionManager type="JDBC" />

    <dataSource type="POOLED">

    <property name="username" value="${username}" />

    <property name="password" value="${password}" />

    <property name="driver" value="${driver}" />

    <property name="url" value="${url}" />

    </dataSource>

    </environment>

    </environments>

     

    <mappers>

    <mapper resource="com/edu/mybatis_spring/mapper/UserMapper.xml" />

    </mappers>

    </configuration>

     

    4. 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="com.edu.mybatis_spring.mapper.UserMapper">

    <!--

    当使用该Mybatis与Spring整合的时候,

    xml文件必须和相应的Mapper接口文件同名,并在同一路径下

    -->

    <insert id="addUser" parameterType="User">

    insert into tb_user(name, age) values(#{name}, #{age})

    </insert>

     

    <select id="getUserByName" resultType="User"

    parameterType="java.lang.String">

    select * from tb_user where name=#{name}

    </select>

    </mapper>

     

    5. User.java

     

    package com.edu.mybatis_spring.model;

     

     

    public class User {

    private int id;

    private String name;

    private int age;

    / 省略getter/setter方法

     

     

    6. UserMapper.java

     

    package com.edu.mybatis_spring.mapper;

     

    import com.edu.mybatis_spring.model.User;

     

     

    public interface UserMapper {

    public void addUser(User user);

    public User getUserByName(String username);

    }

     

    7. UserDaoImpl.java

    package com.edu.mybatis_spring.dao.impl;

     

    import org.apache.ibatis.session.SqlSession;

     

    import com.edu.mybatis_spring.mapper.UserMapper;

    import com.edu.mybatis_spring.model.User;

    import com.edu.mybatis_spring.util.MybatisUtil;

     

     

    public class UserDaoImpl implements UserMapper {

     

     

    @Override

    public void addUser(User user) {

    SqlSession session = null;

     

    try {

    session = MybatisUtil.getSession();

    session.getMapper(UserMapper.class).addUser(user);

    session.commit();

    } catch (Exception e) {

    session.rollback();

    e.printStackTrace();

    } finally {

    MybatisUtil.closeSession(session);

    }

    }

     

     

    @Override

    public User getUserByName(String username) {

    return MybatisUtil.getSession().getMapper(UserMapper.class)

    .getUserByName(username);

    }

     

    }

     

    8. MybatisUtil.java

     

    package com.edu.mybatis_spring.util;

     

    import java.io.IOException;

    import java.io.InputStream;

     

    import org.apache.ibatis.io.Resources;

    import org.apache.ibatis.session.SqlSession;

    import org.apache.ibatis.session.SqlSessionFactory;

    import org.apache.ibatis.session.SqlSessionFactoryBuilder;

     

     

    public class MybatisUtil {

    private static SqlSessionFactory sqlSessionFactory = null;

    / initialized SqlSessionFactory

    static {

    try {

    InputStream is = Resources

    .getResourceAsStream("resources/mybatis-config.xml");

    sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);

    } catch (IOException e) {

    e.printStackTrace();

    }

    }

     

    public static SqlSessionFactory getSqlSessionFactory() {

    return sqlSessionFactory;

    }

     

     

    public static SqlSession getSession() {

    return sqlSessionFactory.openSession();

    }

     

     

    public static void closeSession(SqlSession session) {

    session.close();

    }

    }

     

    9. jdbc.properties

    driver=com.mysql.jdbc.Driver

    url=jdbc:mysql://localhost:3306/mybatis_spring?useUnicode=true&characterEncoding=UTF-8

    username=root

    password=mysql123

    10. Init.sql

    drop database mybatis_spring;

    create database mybatis_spring;

    use mybatis_spring;

     

    drop table tb_user;

    create table tb_user(

    id int auto_increment,

    name varchar(30),

    age int(10),

    primary key(id)

    );

     

    insert into tb_user(id, name, age) values(null, 'zhangsan', 20);

    11. UserDaoImplTest

     

    package com.edu.mybatis_spring.dao.impl;

     

    import org.junit.After;

    import static org.junit.Assert.*;

    import org.junit.Before;

    import org.junit.Test;

     

    import com.edu.mybatis_spring.mapper.UserMapper;

    import com.edu.mybatis_spring.model.User;

     

     

    public class UserDaoImplTest {

     

    private UserMapper userMapper = null;

     

    @Before

    public void setUp() throws Exception {

    userMapper = new UserDaoImpl();

    }

     

     

    @After

    public void tearDown() throws Exception {

    userMapper = null;

    }

     

     

    @Test

    public void testAddUser() {

    User user = new User();

    user.setName("lisi");

    user.setAge(12);

    userMapper.addUser(user);

    assertNotNull("=========== userMapper.addUser(user) failure...", userMapper.getUserByName("lisi"));

    }

     

     

    @Test

    public void testGetUserByName() {

    assertNotNull(userMapper.getUserByName("lisi"));

    }

     

    }

     

    12. 结果

     

    Mybatis3.2和Spring3.x整合----Mybatis+0

     

    Mybatis3.2和Spring3.x整合----Mybatis+0

    12. 分析总结

             这是整合的第一步,暂时只是一个mybatis的工程。学过mybatis的对于这个工程理解很容易。mybatis和hibernate是比较相似的。都可以通过配置文件实现表和实体类的一一映射。只不过mybatis要写的sql还是比较原生态的,根直接操作数据库的sql语句基本一致。mybaits-config.xml文件配置了数据源以及运行需要的环境。当我们把spring整合进来后这个文件也就不需要了。这些配置都可以放在spring的配置文件里了。而且在实现类里也不再需要我们手动管理事务和SqlSessionFactory的管理。这些都可以spring管理。

        更多关于Mybatis的信息请看这里:

    1. http://mybatis.github.io/mybatis-3/zh/

    2. http://code.google.com/p/mybatis/

    3. http://blog.csdn.net/weoln/article/details/5782750

    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    大数据处理系列之(二)系统过载保护
    大数据处理系列之(一)Java线程池使用
    js实现递归菜单无限层
    treeTable实现排序
    spring-dm 一个简单的实例
    Equinox OSGi服务器应用程序的配置步骤 (支持JSP页面)
    Spring DM 2.0 环境配置 解决Log4j问题
    Spring.DM web开发环境搭建
    STL容器的排序
    排序例子
  • 原文地址:https://www.cnblogs.com/ubuntuvim/p/4796548.html
Copyright © 2020-2023  润新知