MyBatis+Oracle简单用户的增加、删除、查询

版权声明:本文为博主原创文章,未经博主允许不得转载,转载请注明博客来源。 https://blog.csdn.net/lvjinhan/article/details/52247464

1.新建一个表T_user,如下:

create table t_user(

t_id number,

t_name varchar2(10),

t_password varchar2(20)

);

2.新建一个包:eduask.pojo,在此包下新建一个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 + "]";

}

}

3.新建一个com.eduask.test包,在此包下新建一个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(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();

}

}

4.新建一个config的源文件,在此源文件下新建一个configuration.xml的配置文件,和新建一个mapper的文件,在mapper文件下新建一个User.xml的配置文件;

4.1 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>

4.2 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>

5.1 用户增加

MyBatisAdd






MyBatis2





5.2 用户查询:


a





5.3用户的删除:

b






c

展开阅读全文

没有更多推荐了,返回首页