1.在oracle数据库中新建一个表:
create table t_user(
t_id number primary key,
t_name varchar2(20),
t_password varchar2(20)
);
insert into t_user values(1,'tom','123456');
insert into t_user values(2,'link','123456');
insert into t_user values(3,'sony','123456');
insert into t_user values(4,'linux','123456');
commit;
2.项目文件介绍
3.Tuser.java;
package com.eduask.pojo;
public class Tuser {
private int t_id;
private String t_name;
private String t_password;
public int getT_id() {
return t_id;
}
public void setT_id(int t_id) {
this.t_id = t_id;
}
public String getT_name() {
return t_name;
}
public void setT_name(String t_name) {
this.t_name = t_name;
}
public String getT_password() {
return t_password;
}
public void setT_password(String t_password) {
this.t_password = t_password;
}
public Tuser(int t_id, String t_name, String t_password) {
super();
this.t_id = t_id;
this.t_name = t_name;
this.t_password = t_password;
}
public Tuser() {
super();
// TODO Auto-generated constructor stub
}
@Override
public String toString() {
return "Tuser [t_id=" + t_id + ", t_name=" + t_name + ", t_password="
+ t_password + "]";
}
}
4.UserTest.java
package com.eduask.test;
import java.io.IOException;
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 com.eduask.pojo.Tuser;
public class UserTest {
//mybatis的用户的增加;
@Test
public void selectUserById() throws IOException {
//将配置文件转换为字节流
InputStream is=Resources.getResourceAsStream("configuration.xml");
//创建SqlSessionFactory工厂
SqlSessionFactory sessionFactory=new SqlSessionFactoryBuilder().build(is);
//创建SqlSession(打开了一个会话)
SqlSession session=sessionFactory.openSession();
Tuser tuser=session.selectOne("selectOneUsers", 3);
System.out.println("查询id=3");
System.out.println(tuser);
session.close();
}
//mybatis的用户的增加;
@Test
public void insertUserById() throws IOException {
//将配置文件转换为字节流
InputStream is=Resources.getResourceAsStream("configuration.xml");
//Reader is=Resources.getResourceAsReader("configuration.xml");
//创建SqlSessionFactory工厂
SqlSessionFactory sessionFactory=new SqlSessionFactoryBuilder().build(is);
//创建SqlSession(打开了一个会话)
SqlSession session=sessionFactory.openSession();
Tuser t=new Tuser();
t.setT_id(1);
t.setT_name("link");
t.setT_password("123456");
int result=session.insert("insertOneUsers", t);
if(result==1){
System.out.println("增加成功");
session.commit();
}else{
System.out.println("增加失败");
}
session.close();
}
//用户删除;
@Test
public void deleteUserById() throws IOException {
//将配置文件转换为字节流
InputStream is=Resources.getResourceAsStream("configuration.xml");
//创建SqlSessionFactory工厂
SqlSessionFactory sessionFactory=new SqlSessionFactoryBuilder().build(is);
//创建SqlSession(打开了一个会话)
SqlSession session=sessionFactory.openSession();
int result=session.delete("deleteOneUsers", 3);
System.out.println(result);
if(result==1){
System.out.println("删除成功");
session.commit();
}else{
System.out.println("删除失败");
}
session.close();
}
}
5.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.eduask.pojo.Tuser">
<!-- 查询一条记录 -->
<select id="selectOneUsers" resultType="com.eduask.pojo.Tuser" parameterType="int">
select * from t_user where t_id=#{id}
</select>
<!-- 增加一条语句 -->
<insert id="insertOneUsers" parameterType="com.eduask.pojo.Tuser">
insert into t_user values(#{t_id},#{t_name},#{t_password})
</insert>
<!--删除一条语句 -->
<delete id="deleteOneUsers" parameterType="int">
delete from t_user where t_id=#{id}
</delete>
</mapper>
6.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>
<!-- <typeAliases>
<typeAlias type="com.cb.Users"/>
</typeAliases> -->
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="oracle.jdbc.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl" />
<property name="username" value="scott" />
<property name="password" value="tiger" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="mapper/Users.xml"/>
</mappers>
</configuration>
7.log4j.propreties
# Properties file for log4j configuration
#
# Place this file in the root CLASSPATH
#
log4j.rootCategory=ERROR,stdout,logfile
#log4j.rootCategory=ERROR,stdout,logfile,Rlogfile
log4j.logger.org.springframework=ERROR
log4j.logger.org.apache.cxf=ERROR
log4j.org.apache.velocity=ERROR
log4j.logger.org.powerise.bl3ee=ERROR
#bl3ee MVC log
log4j.logger.org.powerise.bl3ee.core.mvc.xstruts=ERROR
#bl3ee DAO log
log4j.logger.org.powerise.bl3ee.core.model.dao=DEBUG
#bl3ee ORM log
log4j.logger.org.powerise.bl3ee.core.orm.xibatis=DEBUG
log4j.logger.java.sql.Connection=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.PreparedStatement=DEBUG
log4j.logger.java.sql.ResultSet=DEBUG
#bl3ee UI Tag Library log
log4j.logger.org.powerise.bl3ee.core.tplengine=ERROR
log4j.logger.org.powerise.bl3ee.core.web.taglib.util.TagHelper=ERROR
#request monitor log
log4j.logger.org.powerise.bl3ee.core.web.RequestFilter=ERROR
#JSON encode log
log4j.logger.org.powerise.bl3ee.core.json=ERROR
# Resource manager log
log4j.logger.org.powerise.bl3ee.core.resource=ERROR
# (!!!)Please do not modify the 4 configurations
log4j.logger.org.powerise.bl3ee.core.web.SystemInitListener=INFO
log4j.logger.org.powerise.bl3ee.core.server.BL3Server=INFO
log4j.logger.org.powerise.bl3ee.core.model.SpringBeanLoader=INFO
log4j.logger.org.powerise.bl3ee.core.model.SpringBeanAspect=ERROR
#stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %5p [%t] - %m%n
#logfile
log4j.appender.logfile=org.apache.log4j.DailyRollingFileAppender
log4j.appender.logfile.DatePattern=yyyy-MM-dd
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - <%m>%n
log4j.appender.logfile.File=c:/BL3Logs/bl3ee.log
#Rlogfile
log4j.appender.Rlogfile=org.apache.log4j.RollingFileAppender
log4j.appender.Rlogfile.layout=org.apache.log4j.PatternLayout
log4j.appender.Rlogfile.layout.ConversionPattern=%d %p [%c] - <%m>%n
log4j.appender.Rlogfile.File=c:/BL3Logs/bl3ee.log
log4j.appender.Rlogfile.MaxFileSize=2048KB
log4j.appender.Rlogfile.MaxBackupIndex=100
8.效果如下: