Ibatis基本的增删改查操作

part_1: SqlMapConfig.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMapConfig PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-config-2.dtd">
<sqlMapConfig>

    <properties resource="cn/mike/ibatis/primer/config/SqlMap.properites" />

    <transactionManager type="JDBC">
        <dataSource type="SIMPLE">
            <property name="JDBC.Driver" value="${driverClass}" />
            <property name="JDBC.ConnectionURL" value="${url}" />
            <property name="JDBC.Username" value="${username}" />
            <property name="JDBC.Password" value="${password}" />
        </dataSource>
    </transactionManager>

    <sqlMap resource="cn/mike/ibatis/primer/sqlmap/User-Operation.xml" />


</sqlMapConfig>

part_2:SqlMap.properites

driverClass=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/ibatis_test
username=root
password=3713

part_3:User-Operation.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMap        
    PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"        
    "http://ibatis.apache.org/dtd/sql-map-2.dtd">

<sqlMap>

    <typeAlias alias="User" type="cn.mike.ibatis.primer.pojo.User" />

    <select id="queryAllUsers" resultClass="User">
        SELECT * FROM USERS;
    </select>

    <select id="queryUserById" parameterClass="int" resultClass="User">
        SELECT * FROM USERS WHERE ID=#ID#;
    </select>

    <insert id="addUser" parameterClass="User">
        INSERT INTO USERS (ID,USERNAME,PASSWORD) VALUES(#id#,#username#,#password#);
    </insert>

    <delete id="deleteUserById" parameterClass="int">
        DELETE FROM USERS WHERE ID = #id#;
    </delete>

    <update id="updateUser" parameterClass="User">
        UPDATE USERS SET ID=#id#,USERNAME=#username#,PASSWORD=#password# WHERE ID=#id#;
    </update>

    <select id="queryUserByName" parameterClass="String" resultClass="User">
        SELECT * FROM USERS WHERE USERNAME LIKE '%$username$%';
    </select>


</sqlMap>

part_4:User.java

package cn.mike.ibatis.primer.pojo;

public class User {

    private int id;
    private String username;
    private String password;

    public User() {
        super();
    }

    public User(int id, String username, String password) {
        super();
        this.id = id;
        this.username = username;
        this.password = password;
    }

    @Override
    public String toString() {
        return "User [id=" + id + ", password=" + password + ", username=" + username + "]";
    }

    public int getId() {
        return id;
    }

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

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

}

part_5:UserDao.java

package cn.mike.ibatis.primer.dao;

import cn.mike.ibatis.primer.pojo.User;

import java.util.List;

public interface UserDao {

    public void addUser(User user);

    public void addUserBySequence(User user);

    public int deleteUserById(int id);

    public int updateUser(User user);

    public List<User> queryAllUsers();

    public List<User> queryUserByName(String name);

    public User queryUserById(int id);

}

part_6:UserDaoImpl.java

package cn.mike.ibatis.primer.dao.impl;

import java.sql.SQLException;
import java.util.List;

import com.ibatis.sqlmap.client.SqlMapClient;

import cn.mike.ibatis.primer.dao.UserDao;
import cn.mike.ibatis.primer.pojo.User;
import cn.mike.ibatis.primer.utils.SqlMapUtils;

public class UserDaoImpl implements UserDao {

    private SqlMapClient client;

    {
        client = SqlMapUtils.getSqlMapClient();
    }

    public void addUser(User user) {
        try {
            this.client.insert("addUser", user);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void addUserBySequence(User user) {

    }

    public int deleteUserById(int id) {
        try {
            return this.client.delete("deleteUserById", id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return id;

    }

    @SuppressWarnings("unchecked")
    public List<User> queryAllUsers() {

        List<User> userList = null;
        try {
            userList = this.client.queryForList("queryAllUsers");
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return userList;
    }

    public User queryUserById(int id) {

        try {
            return (User) client.queryForObject("queryUserById", id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @SuppressWarnings("unchecked")
    public List<User> queryUserByName(String name) {
        try {
            return (List<User>) this.client.queryForList("queryUserByName", name);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public int updateUser(User user) {
        try {
            return this.client.update("updateUser", user);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

}

part_7:SqlMapUtils.java

package cn.mike.ibatis.primer.utils;

import java.io.IOException;
import java.io.Reader;

import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;

public class SqlMapUtils {

    private static SqlMapClient sqlMapClient;

    static {
        try {
            Reader reader = Resources.getResourceAsReader("SqlMapConfig.xml");
            sqlMapClient = SqlMapClientBuilder.buildSqlMapClient(reader);
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static SqlMapClient getSqlMapClient() {
        return sqlMapClient;
    }

}

part_8:JUnitTest.java

package cn.mike.ibatis.primer;

import org.junit.Test;

import cn.mike.ibatis.primer.dao.UserDao;
import cn.mike.ibatis.primer.dao.impl.UserDaoImpl;
import cn.mike.ibatis.primer.pojo.User;

public class JUnitTest {

    @Test
    public void testUserService() {

        UserDao userDao = new UserDaoImpl();

        // queryAllUsers
        if (null != userDao.queryAllUsers()) {
            for (User eachUser : userDao.queryAllUsers()) {
                System.out.println(eachUser);
            }
        }
        System.out.println();

        // queryUserById
        if (null != userDao.queryUserById(1)) {
            System.out.println(userDao.queryUserById(1));
        }
        System.out.println();

        // addUser
        final int michaelsID = 4;
        // userDao.addUser(new User(michaelsID, "michael", "jackson"));

        // queryUserById
        if (null != userDao.queryUserById(michaelsID)) {
            System.out.println(userDao.queryUserById(michaelsID));
        }

        // deleteUserById
        System.out.println(userDao.deleteUserById(michaelsID) + " row(s) affected.\n");

        // updateUser
        System.out.println(userDao.updateUser(new User(1, "sun", "3716")) + " row(s) affected.\n");

        // queryUserByName
        if (null != userDao.queryUserByName("Sun")) {
            for (User each : userDao.queryUserByName("mike")) {
                System.out.println(each);
            }
        }
        System.out.println();



    }// end method - testUserService
}// end class - JUnitTest

// /"=====================SEPARATOR====================="/

包路径

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值