代码改变世界

mybatis 框架 的简单使用

2017-02-04 16:01  甘雨路  阅读(143)  评论(0编辑  收藏  举报
# Global logging configuration
#在开发环境下日志级别要设置成DEBUG,生产环境设置成info或error
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
<?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事务管理,事务控制由mybatis -->
            <transactionManager type="JDBC"/>
            <!-- 数据库连接池,由mybatis管理 -->
            <dataSource type="POOLED">
                <property name="driver" value="oracle.jdbc.driver.OracleDriver"/>
                <property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl"/>
                <property name="username" value="LF"/>
                <property name="password" value="LF"/>
            </dataSource>
        </environment>
    </environments>
    
    <!-- 加载映射文件 -->
    <mappers>
        <mapper resource="config/sqlmap/user.xml"/>
    </mappers>
    
</configuration>
<?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">
    <select id="findUserByName" parameterType="java.lang.String" resultType="com.zr.testmybatis.entity.User">
        SELECT * FROM USERTEST WHERE NAME=#{NAME}
    </select>
    
    <insert id="insertUser" parameterType="com.zr.testmybatis.entity.User">
        INSERT INTO USERTEST VALUES(#{name},#{age})
    </insert>
    
    <delete id="deleteUser" parameterType="java.lang.String">
        DELETE FROM USERTEST WHERE NAME=#{name} 
    </delete>
    
    <update id="updateUser" parameterType="com.zr.testmybatis.entity.User">
        UPDATE USERTEST SET AGE=#{age} WHERE NAME=#{name}
    </update>
    
</mapper>
package com.zr.testmybatis.entity;

public class User {
    
    
    private String name;
    private int age;
    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 User() {
        super();
    }
    public User(String name, int age) {
        super();
        this.name = name;
        this.age = age;
    }
    
    @Override
    public String toString() {
        return "User [name=" + name + ", age=" + age + "]";
    }
    
}
package com.zr.testmybatis.test;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Delayed;

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 com.zr.testmybatis.entity.User;


public class TestA {
    
    public static void main(String[] args) {
        
        //mybatis配置文件路径
        String resource = "config/SqlMapConfig.xml";
        // 获取配置文件流
        InputStream inputStream = null;
        try {
            inputStream = Resources.getResourceAsStream(resource);
        } catch (IOException e) {
            e.printStackTrace();
        }
        //创建会话工厂,传入mybatis的配置文件信息
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        
        //通过工厂得到SqlSession
        SqlSession sqlSession = sqlSessionFactory.openSession();
        
        List<User> list = new ArrayList<User>();
        
        // 通过SqlSession操作数据库
        //查询
        /*list = sqlSession.selectList("test.findUserByName", "lf");
        System.out.println(list);*/
        
        // 插入
        /*User user = new User("tl", 23);
        int i = sqlSession.insert("test.insertUser", user);
        sqlSession.commit();
        System.out.println(i);*/
        
        //删除
        /*int i = sqlSession.delete("test.deleteUser", "lf");
        sqlSession.commit();
        System.out.println(i);*/
        
        User user = new User("tl", 21);
        int i = sqlSession.update("test.updateUser", user);
        sqlSession.commit();
        System.out.println(i);
        
        sqlSession.close();
    }
    
}