实体类 user
public class User {
private Integer id;
private String name;
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;
}
}
mybatis配置文件 configuration.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>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="oracle.jdbc.driver.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:@192.168.0.11:1521:ORCL" />
<property name="username" value="dylog" />
<property name="password" value="dylog" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/hq/model/User.xml" />
</mappers>
</configuration>
user类的mybatis配置文件 User.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.hq.model.User">
<!-- 添加一条记录 -->
<insert id="insertvalues" parameterType="com.hq.model.User">
insert into
users(id,name) values(auto_id.nextval,#{name})
</insert>
<!-- 删除一条记录 -->
<delete id="deleteOneUsers" parameterType="com.hq.model.User">
delete from users
where id=#{id}
</delete>
<!-- 更新一条记录 -->
<update id="updateOneUsers" parameterType="com.hq.model.User">
update users set
name=#{name} where id=#{id}
</update>
<!-- 查询一条记录 -->
<select id="selectOneUsers" resultType="com.hq.model.User"
parameterType="int">
select * from users where id = #{id}
</select>
<!-- 查询全部记录 -->
<select id="selectAllUsers" resultType="com.hq.model.User"
parameterType="int">
select * from users
</select>
</mapper>
mybatis工厂类 SqlUtil
import java.io.IOException;
import java.io.InputStream;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
public class SqlUtil {
//session工厂
private static SqlSessionFactory sqlmapper = null;
public SqlSessionFactory getSqlSessionFactory() throws IOException{
if(sqlmapper == null){
String resource = "configuration.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlmapper = new SqlSessionFactoryBuilder().build(inputStream);
}
return sqlmapper;
}
}
最后一个测试类 Test.java
import java.io.IOException;
import java.util.ArrayList;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import com.hq.model.User;
import com.hq.util.SqlUtil;
public class Test {
public static void main(String[] args) throws IOException {
SqlSessionFactory factory = new SqlUtil().getSqlSessionFactory();
SqlSession session = factory.openSession();
try {
// 增加一条新的记录
/*User users=new User(); users.setName("chnebei");
session.insert("com.hq.model.User.insertvalues",users);
session.commit();*/
// 删除一条记录 oracle可以使用
/*
* session.delete("com.hq.model.User.deleteOneUsers",0);
* session.commit();
*/
// 更新一条数据 oracle可以使用
/*
* Users users=new Users(); users.setName("lisi"); users.setId(10);
* session.update("com.hq.model.User.updateOneUsers",users);
* session.commit();
*/
// 按条件查询一条数据 oracle可以使用
/*
* Users users = (Users)
* session.selectOne("com.hq.model.User.selectOneUsers",10);
* System.out.println(users.getId()+"\t"+users.getName());
*/
// 查询表的全部数据 oracle可以使用
ArrayList<User> list = (ArrayList<User>) session.selectList("com.hq.model.User.selectAllUsers");
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i).getId() + "\t"+ list.get(i).getName());
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if(session!=null){
session.close();
}
}
}
}