1. 程式人生 > 其它 >java——mybatis——Mybatis的CRUD——獲取返回自增id

java——mybatis——Mybatis的CRUD——獲取返回自增id

========================================================================================================

===================================================================================================================

在第一個xml示例專案的基礎上,進行操作:

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>
    <!-- 配置mybatis執行環境 -->
    <environments default="development">
        <
environment id="development"> <!-- 使用JDBC的事務管理 --> <transactionManager type="JDBC" /> <dataSource type="POOLED"> <!-- MySQL資料庫驅動 --> <property name="driver" value="com.mysql.jdbc.Driver" /> <!--
連線資料庫的URL --> <property name="url" value="jdbc:mysql://localhost:3306/mysql8?characterEncoding=utf8" /> <property name="username" value="root" /> <property name="password" value="123456" /> </dataSource> </environment> </environments> <!-- 將mapper檔案加入到配置檔案中 --> <mappers> <mapper resource="com/sunxl/dao/UserDao.xml"/> </mappers> </configuration>

POJO類不變:

package com.sunxl.pojo;

public class User {

    Integer id;
    String name;
    Integer age;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Users{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

}

修改mapper.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.sunxl.dao.UserDao">
    <!-- 查詢所有網站資訊 -->
    <select id="findAll" resultType="com.sunxl.pojo.User">
        select * from USERs
    </select>

    <insert id="saveUser" parameterType="com.sunxl.pojo.User">
            insert into users(id,name,age) values(#{id},#{name},#{age})
    </insert>

    <update id="updateUser" parameterType="com.sunxl.pojo.User">
            update users set name = #{name},age = #{age} where id = #{id}
    </update>

    <delete id="deleteUser" parameterType="java.lang.Integer">
            delete from users where id = #{id}
    </delete>

    <select id="findById" parameterType="java.lang.Integer" resultType="com.sunxl.pojo.User">
        select * from USERs where id = #{id}
    </select>

    <select id="findByName" parameterType="java.lang.String" resultType="com.sunxl.pojo.User">
        select * from USERs where name like '%${value}%';
    </select>

    <select id="findTotal" resultType="int">
        select count(id) from USERs
    </select>


    <insert id="saveInsertUser" parameterType="com.sunxl.pojo.User">
        <selectKey keyProperty="id" keyColumn="id" order="AFTER" resultType="int">
            SELECT LAST_INSERT_ID();
        </selectKey>
        insert into users(id,name,age) values(#{id},#{name},#{age})
    </insert>

</mapper>

修改DAO介面檔案:

package com.sunxl.dao;

import com.sunxl.pojo.User;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface UserDao {

    List<User> findAll();

    int saveUser(User user);

    int updateUser(User user);

    int deleteUser(Integer id);

    User findById(Integer id);

    List<User> findByName(String name);

    int findTotal();

    int saveInsertUser(User user);

}

執行:

import com.sunxl.dao.UserDao;
import com.sunxl.pojo.User;
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;
import java.util.List;

public class MybatisTest {

    public static void main(String[] args) throws IOException {

//        InputStream config = Resources.getResourceAsStream("mybatis-config.xml");
//
//        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
//
//        SqlSessionFactory factory = builder.build(config);
//
//        SqlSession ss = factory.openSession();
//
//        UserDao userdao = ss.getMapper(UserDao.class);
//
//        List<User> users = userdao.findAll();
//
//        for (User user: users){
//            System.out.println(user);
//        }
//
//        ss.commit();
//
//        //4. 釋放資源
//        ss.close();
//
//        config.close();

        InputStream config = Resources.getResourceAsStream("mybatis-config.xml");

        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();

        SqlSessionFactory factory = builder.build(config);

        SqlSession ss = factory.openSession();


        User user = new User();
        user.setName("s-s-x-1002");
        user.setAge(1002);


        System.out.println(user);


        UserDao userdao = ss.getMapper(UserDao.class);

        userdao.saveInsertUser(user);

        System.out.println(user);

        ss.commit();

        //4. 釋放資源
        ss.close();

        config.close();

    }
}