码迷,mamicode.com
首页 > 其他好文 > 详细

MyBatis 开发记录

时间:2017-09-03 18:42:02      阅读:134      评论:0      收藏:0      [点我收藏+]

标签:3.0   row   env   pool   logging   工厂   efault   output   att   

1、在classpath下创建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

 

2、在classpath下创建SqlMapConfig.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>

  <!-- spring整合后 environments配置将废除-->

  <environments default="development">

    <environment id="development">

      <!-- 使用jdbc事务管理-->

      <transactionManager type="JDBC" />

      <!-- 数据库连接池-->

      <dataSource type="POOLED">

        <property name="driver" value="com.mysql.jdbc.Driver" />

        <property name="url" value="jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8" />

        <property name="username" value="root" />

        <property name="password" value="mysql" />

      </dataSource>

    </environment>

  </environments>

</configuration>

3、创建与数据库对应的PO类,PO类用于mybatis的sql映射

4、在classpath下的sqlmap目录下创建sql映射文件User.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="test">

  <!-- 根据id获取用户信息 -->

  <select id="findUserById" parameterType="int" resultType="cn.itcast.mybatis.po.User">

    select * from user where id = #{id}

  </select>

  <!-- 自定义条件查询用户列表 -->

  <select id="findUserByUsername" parameterType="java.lang.String" 

    resultType="cn.itcast.mybatis.po.User">

     select * from user where username like ‘%${value}%‘

  </select>

 

</mapper>

5、加载映射文件

<mappers>

 

  <mapper resource="sqlmap/User.xml"/>

 

</mappers>

6、测试程序

public class Mybatis_first {
    
    //会话工厂
    private SqlSessionFactory sqlSessionFactory;

    @Before
    public void createSqlSessionFactory() throws IOException {
        // 配置文件
        String resource = "SqlMapConfig.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);

        // 使用SqlSessionFactoryBuilder从xml配置文件中创建SqlSessionFactory
        sqlSessionFactory = new SqlSessionFactoryBuilder()
                .build(inputStream);

    }

    // 根据 id查询用户信息
    @Test
    public void testFindUserById() {
        // 数据库会话实例
        SqlSession sqlSession = null;
        try {
            // 创建数据库会话实例sqlSession
            sqlSession = sqlSessionFactory.openSession();
            // 查询单个记录,根据用户id查询用户信息
            User user = sqlSession.selectOne("test.findUserById", 10);
            // 输出用户信息
            System.out.println(user);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (sqlSession != null) {
                sqlSession.close();
            }
        }

    }

    // 根据用户名称模糊查询用户信息
    @Test
    public void testFindUserByUsername() {
        // 数据库会话实例
        SqlSession sqlSession = null;
        try {
            // 创建数据库会话实例sqlSession
            sqlSession = sqlSessionFactory.openSession();
            // 查询单个记录,根据用户id查询用户信息
            List<User> list = sqlSession.selectList("test.findUserByUsername", "张");
            System.out.println(list.size());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (sqlSession != null) {
                sqlSession.close();
            }
        }

    }
}

 

MyBatis 开发记录

标签:3.0   row   env   pool   logging   工厂   efault   output   att   

原文地址:http://www.cnblogs.com/m2492565210/p/7470359.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!