maven 搭建springMvc+mybatis

2018-07-06 01:24:50来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

 

1、在resource文件夹下创建Configure.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>
    <!--<typeAliases>
        <typeAlias alias="Test" type="com.jd.mybatis.bean.Test" />
    </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:3306/dss" />
                <property name="username" value="root" />
                <property name="password" value="123" />
            </dataSource>
        </environment>
    </environments>

    <mappers>
        <!-- // power by http://www.yiibai.com -->
        <mapper resource="config/Test.xml" />
    </mappers>
</configuration>

  

2、创建Test的数据表映射文件Test.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="TestMapper">
    <select id="selectUserByID" parameterType="int" resultType="com.text.Test">
        select * from `test` where id = #{id}
    </select>

    <select id="findAll" resultType="com.text.Test">
        select * from `test`
    </select>
</mapper>

  

项目结构

若是没有resource文件夹 可直接新建,然后通过以下步骤变为资源文件夹

 

 创建MyBatisUtil工具类

package config;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

public class MyBatisUtil {
    public  static SqlSessionFactory sqlSessionFactory;
    public  static   ThreadLocal<SqlSession>  tl=new  ThreadLocal<SqlSession>();
    static{
        InputStream stream=null;
        try {
            //读取mybatis-config配置文件
            stream = Resources.getResourceAsStream("config/Configure.xml");
            //创建SqlSessionFactory对象
            sqlSessionFactory= new SqlSessionFactoryBuilder().build(stream);
        } catch (IOException e) {
            e.printStackTrace();
            throw  new  RuntimeException("读取配置文件失败");
        }finally{
            try {
                stream.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    //获取SqlSession对象
    public   static   SqlSession   openSqlSession(){
        SqlSession sqlSession = tl.get();
        if(sqlSession==null){
            sqlSession=sqlSessionFactory.openSession();
            tl.set(sqlSession);
        }
        return  sqlSession;
    }


    public   static   SqlSession   openSqlSession(boolean isAutoCommit){
        SqlSession sqlSession = tl.get();
        if(sqlSession==null){
            sqlSession=sqlSessionFactory.openSession(isAutoCommit);
            tl.set(sqlSession);
        }
        return  sqlSession;
    }
}

  

项目结构

直接 测试

 public   List<Test>  getTest(){
        SqlSession sqlSession = MyBatisUtil.openSqlSession(false);
        List<Test>  user =  sqlSession.selectList("TestMapper.findAll");
        sqlSession.commit();
        sqlSession.close();
        return user;
    }

  

至此mybatis创建成功。

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:Java中的值传递和引用传递

下一篇:MyBatis 获取插入记录的 id