mybatis一(常用配置信息和获取插入后id)


"1.0" encoding="UTF-8" ?> DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> "db.properties"> "mapUnderscoreToCamelCase" value="true"/> "jdbcTypeForNull" value="NULL"> "lazyLoadingEnabled" value="true"> "aggressiveLazyLoading" value="false"> "cacheEnabled" value="false"> "model.User" alias="user"> "model"> default="development"> "development"> "JDBC"/> "POOLED"> "driver" value="${jdbc.driver}" /> "url" value="${jdbc.url}" /> "username" value="${jdbc.username}" /> "password" value="${jdbc.password}" /> "test"> "JDBC"/> "POOLED"> "driver" value="${oracle.driver}" /> "url" value="${oracle.url}" /> "username" value="${oracle.username}" /> "password" value="${oracle.password}" /> "DB_VENDOR"> "MySQL" value="mysql"> "Oracle" value="oracle"> "UserMapper.xml"/>

 二、mapper配置

"1.0" encoding="UTF-8" ?>
DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

namespace="mapper.UserMapper">
    
    <select id="findUserById" parameterType="java.lang.Integer" resultType="model.User" databaseId="mysql">
        SELECT * FROM user WHERE id=#{id}
    select>
    
    
    <select id="findUserByUserName" parameterType="java.lang.String" resultType="model.User">
        SELECT * FROM user WHERE username LIKE '%${value}%'
    select>
    
    
    "insertUser" parameterType="model.User" useGeneratedKeys="true" keyProperty="id">
        
       
  

INSERT INTO user (id,last_name,email,gender) VALUES (#{id},#{lastName},#{email},#{gender}) "deleteUserById" parameterType="java.lang.Integer"> DELETE FROM user WHERE id=#{id} "updateUserById" parameterType="model.User"> UPDATE user SET username=#{username} WHERE id=#{id}

三、测试代码

package test;

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;
import org.junit.Test;
import mapper.UserMapper;
import model.User;

public class MybatisTest {

    @Test
    public void test() {
        String resource = "mybatis-config.xml";
        InputStream inputStream;
        try {
            inputStream = Resources.getResourceAsStream(resource);
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
            SqlSession sqlSession = sqlSessionFactory.openSession();
            UserMapper mapper = sqlSession.getMapper(UserMapper.class);
            User user = new User();
            user.setLastName("赵四222");
            user.setEmail("1232342221");
            user.setGender("北京昌平222");
            mapper.insertUser(user);
            System.out.println(user.toString());
            sqlSession.commit();
            // 释放资源
            sqlSession.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Test
    public void testFindUserById() throws Exception {
        String resource = "mybatis-config.xml";
        // 通过流将核心配置文件读取进来
        InputStream inputStream = Resources.getResourceAsStream(resource);
        // 通过核心配置文件创建会话工厂
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(inputStream);

        // 通过会话工厂创建会话
        SqlSession session = factory.openSession();

        User user = session.selectOne("mapper.UserMapper.findUserById", 1);
        System.out.println(user.toString());
    }

    @Test
    public void testInsertUser() throws Exception {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(inputStream);

        SqlSession session = factory.openSession();
        User user = new User();
        user.setLastName("赵四");
        user.setEmail("1232341");
        user.setGender("北京昌平");
        System.out.println(user.getId());
        session.insert("test.insertUser", user);
        // mybatis中事务默认是开启的,所以在mybatis中不需要显式开启事务,只需要显式提交事务
        // 提交事务
        session.commit();
        System.out.println(user.getId());
    }
}

 

代码

你可能感兴趣的:(mybatis一(常用配置信息和获取插入后id))