新建maven工程:
java代码:
package com.ibatis.dao;
import java.util.List;
import com.ibatis.entity.User;
public interface IUserDao {
void insertUser(User user);
void deleteUserById(int id);
void updateUser(User user);
User selectUserById(int id);
List<User> selectAllUser();
}
package com.ibatis.dao.impl;
import java.io.IOException;
import java.io.Reader;
import java.sql.SQLException;
import java.util.List;
import com.ibatis.common.resources.Resources;
import com.ibatis.dao.IUserDao;
import com.ibatis.entity.User;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;
public class UserDao implements IUserDao {
private static SqlMapClient sqlMapClient = null;
static {
try {
Reader reader = Resources.getResourceAsReader("com/ibatis/entity/SqlMapConfig.xml");
sqlMapClient = SqlMapClientBuilder.buildSqlMapClient(reader);
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public void insertUser(User user) {
try {
sqlMapClient.insert("addUser", user);
} catch (SQLException e) {
e.printStackTrace();
}
}
public void deleteUserById(int id) {
try {
sqlMapClient.delete("deleteUserById", id);
} catch (SQLException e) {
e.printStackTrace();
}
}
public void updateUser(User user) {
try {
sqlMapClient.update("updateUser", user);
} catch (SQLException e) {
e.printStackTrace();
}
}
public User selectUserById(int id) {
User user = null;
try {
user = (User) sqlMapClient.queryForObject("selectUserById", id);
} catch (SQLException e) {
e.printStackTrace();
}
return user;
}
@SuppressWarnings("unchecked")
public List<User> selectAllUser() {
List<User> userList = null;
try {
userList = (List<User>) sqlMapClient.queryForList("selectAllUser");
} catch (SQLException e) {
e.printStackTrace();
}
return userList;
}
}
package com.ibatis.entity;
public class User {
private int id;
private String username;
private int age;
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 int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "User [id=" + id + ", username=" + username + ", age=" + age + "]";
}
}
配置文件
com/ibatis/entity/User.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="com.ibatis.entity.User" />
<select id="selectAllUser" resultClass="User">
select id, user_name username, user_age age from tb_user
</select>
<select id="selectUserById" parameterClass="int" resultClass="User">
select id, user_name username, user_age age from tb_user where id=#id#
</select>
<insert id="addUser" parameterClass="User">
insert into tb_user(id,user_name,user_age) values (#id#, #username#,#age#)
</insert>
<delete id="deleteUserById" parameterClass="int">
delete from tb_user where id=#id#
</delete>
<update id="updateUser" parameterClass="User">
update tb_user set user_name=#username#, user_age=#age# where id=#id#
</update>
</sqlMap>
com/ibatis/entity/SqlMapConfig.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMapConfig
PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-config-2.dtd">
<sqlMapConfig>
<transactionManager type="JDBC" commitRequired="false">
<dataSource type="SIMPLE">
<property name="JDBC.Driver" value="oracle.jdbc.driver.OracleDriver" />
<property name="JDBC.ConnectionURL" value="jdbc:oracle:thin:@localhost:1521:ORCL" />
<property name="JDBC.Username" value="HIBERNATE" />
<property name="JDBC.Password" value="HIBERNATE" />
</dataSource>
</transactionManager>
<sqlMap resource="com/ibatis/entity/User.xml" />
</sqlMapConfig>
测试类:
package com.ibatis.test;
import java.util.List;
import com.ibatis.dao.IUserDao;
import com.ibatis.dao.impl.UserDao;
import com.ibatis.entity.User;
import junit.framework.TestCase;
public class TestUser extends TestCase {
private IUserDao userDao = new UserDao();
public void testInsert() {
User user = new User();
user.setId(1);
user.setUsername("user1");
user.setAge(18);
userDao.insertUser(user);
}
public void testUpdate() {
User user = new User();
user.setId(1);
user.setUsername("user11");
user.setAge(18);
userDao.updateUser(user);
}
public void testSelectById() {
User user = userDao.selectUserById(1);
System.out.println(user);
}
public void testSelectAllUser() {
List<User> userList = userDao.selectAllUser();
for (User user: userList) {
System.out.println(user);
}
}
public void testDelete() {
userDao.deleteUserById(1);
}
}
package com.ibatis.test;
import junit.framework.Test;
import junit.framework.TestSuite;
public class TestAll {
public static Test suite() {
TestSuite suite = new TestSuite(TestUser.class.getSimpleName());
suite.addTestSuite(TestUser.class);
return suite;
}
}
pom文件:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>ibatis</groupId>
<artifactId>ibatis</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.apache.ibatis</groupId>
<artifactId>ibatis-core</artifactId>
<version>3.0</version>
</dependency>
<dependency>
<groupId>org.apache.ibatis</groupId>
<artifactId>ibatis-sqlmap</artifactId>
<version>2.3.4.726</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc14</artifactId>
<version>10.2.0.1.0</version>
</dependency>
</dependencies>
</project>
SQL:
CREATE TABLE TB_USER
(
ID INTEGER PRIMARY KEY,
USER_NAME VARCHAR2(20) NOT NULL,
USER_AGE INTEGER NOT NULL
);
标签:改查,public,ibatis,user,增删,import,id,User From: https://blog.51cto.com/u_1002776/6229001