mysql接口用例增删改查_Mybatis通过接口的方式实现增删改查

导入jar包

【mybatis】

bcfb1c6bc2ca8e4941d04b004a726d23.png

【oracle】

f53543b5df2f140fe41eb66aed735cd5.png

生成数据库

70f4d385e56c84fb6e7beba02ae3a948.png

1、添加Mybatis的配置文件mybatis-config.xml

在src目录下创建一个mybatis-config.xml文件,如下图所示:

17ab91435ac472a89aa193cf646a4f0e.png

/p>

PUBLIC "-//mybatis.org//DTD Config 3.0//EN"

"http://mybatis.org/dtd/mybatis-3-config.dtd">

db.properties如下:

jdbc.username=root

jdbc.password=123

jdbc.url=jdbc:oracle:thin:@localhost:1521:orcl

jdbc.driverClass=oracle.jdbc.OracleDriver

2、定义表所对应的实体类,如下图所示:

8be7d87f479964f64d66f5228fe5d111.png

packagecom.model;//Generated 2017-4-19 10:19:42 by Hibernate Tools 5.2.0.CR1

importjava.math.BigDecimal;importorg.apache.ibatis.type.Alias;/*** TestId generated by hbm2java*/

public classTestId {privateBigDecimal id;privateString username;privateString password;publicTestId() {

}publicTestId(BigDecimal id, String username, String password) {this.id =id;this.username =username;this.password =password;

}publicBigDecimal getId() {return this.id;

}public voidsetId(BigDecimal id) {this.id =id;

}publicString getUsername() {return this.username;

}public voidsetUsername(String username) {this.username =username;

}publicString getPassword() {return this.password;

}public voidsetPassword(String password) {this.password =password;

}public booleanequals(Object other) {if ((this ==other))return true;if ((other == null))return false;if (!(other instanceofTestId))return false;

TestId castOther=(TestId) other;return ((this.getId() ==castOther.getId())|| (this.getId() != null && castOther.getId() != null && this.getId().equals(castOther.getId())))&& ((this.getUsername() == castOther.getUsername()) || (this.getUsername() != null

&& castOther.getUsername() != null && this.getUsername().equals(castOther.getUsername())))&& ((this.getPassword() == castOther.getPassword()) || (this.getPassword() != null

&& castOther.getPassword() != null && this.getPassword().equals(castOther.getPassword())));

}public inthashCode() {int result = 17;

result= 37 * result + (getId() == null ? 0 : this.getId().hashCode());

result= 37 * result + (getUsername() == null ? 0 : this.getUsername().hashCode());

result= 37 * result + (getPassword() == null ? 0 : this.getPassword().hashCode());returnresult;

}

@OverridepublicString toString() {return "TestId [id=" + id + ", username=" + username + ", password=" + password + "]";

}

}

packagecom.model;importjava.util.Date;public classTestInfo {privateInteger ids;privateTestId testId;privateString address;privateDate birthday;publicInteger getIds() {returnids;

}public voidsetIds(Integer ids) {this.ids =ids;

}publicTestId getTestId() {returntestId;

}public voidsetTestId(TestId testId) {this.testId =testId;

}publicString getAddress() {returnaddress;

}public voidsetAddress(String address) {this.address =address;

}publicDate getBirthday() {returnbirthday;

}public voidsetBirthday(Date birthday) {this.birthday =birthday;

}publicTestInfo(Integer ids, TestId testId, String address, Date birthday) {super();this.ids =ids;this.testId =testId;this.address =address;this.birthday =birthday;

}publicTestInfo() {super();

}

@OverridepublicString toString() {return "TestInfo [ids=" + ids + ", testId=" + testId + ", address=" + address + ", birthday=" + birthday + "]";

}

}

3、定义操作test表的sql映射文件

3b1ceed1f49deec59eae3308378d0d92.png

UserInfoMapper.java接口如下:

packagecom.dao;importjava.util.List;importcom.model.TestInfo;public interfaceUserInfoMapper {public Listselect();

}

UserMapper.java接口如下:

packagecom.dao;importjava.util.List;importjava.util.Map;importcom.model.TestId;public interfaceUserMapper {publicInteger add(TestId ti);publicInteger delete(Integer id);publicInteger update(TestId ti);publicTestId select(Integer id);public List selectlist(Mapmap);

}

UserInfoMapper.xml如下:

/p>

PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"

"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

select * from testinfo ti left join test t on ti.id=t.id

UserMapper.xml如下:

/p>

PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"

"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

insert into test values(sq_mybatis.nextval,#{username},#{password})

delete test t where t.id=#{id}

update test t set t.username=#{username},t.password=#{password} where t.id=#{id}

select * from test t where t.id=#{id}

select * from test t where t.username like #{username} and t.password like #{password}

4、创建一个MybatisUtil的和Junit的类,来进行测试

8e090c2d052dc3b6a1810e342308bed6.png

MybatisUtil.java如下:

packagecom.util;importjava.io.IOException;importjava.io.InputStream;importorg.apache.ibatis.io.Resources;importorg.apache.ibatis.session.SqlSession;importorg.apache.ibatis.session.SqlSessionFactory;importorg.apache.ibatis.session.SqlSessionFactoryBuilder;/*** mybatis工具类

*@authorAdministrator

**/

public classMybatisUtil {private staticSqlSessionFactory ssf;private staticSqlSession ss;/*** 获取mybatis核心sqlsessionfactory

*@return

*/

private staticSqlSessionFactory getSqlSessionFctory(){

InputStream it= null;try{

it= Resources.getResourceAsStream("mybatis-config.xml");

ssf= newSqlSessionFactoryBuilder().build(it);

ss=ssf.openSession();

}catch(IOException e) {//TODO Auto-generated catch block

e.printStackTrace();

}returnssf;

}/*** 获取sqlsession

*@return

*/

public staticSqlSession getSqlSession(){

ss=getSqlSessionFctory().openSession();returnss;

}public static voidmain(String[] args){

System.out.println(getSqlSession());

}

}

Junit.java如下:

packagecom.util;import static org.junit.Assert.*;importjava.math.BigDecimal;importjava.util.HashMap;importjava.util.List;importjava.util.Map;importorg.apache.ibatis.session.SqlSession;importorg.junit.After;importorg.junit.Before;importorg.junit.Test;importcom.dao.UserMapper;importcom.model.TestId;public classJunit {privateSqlSession ss;privateUserMapper um;

@Beforepublic void setUp() throwsException {

ss=MybatisUtil.getSqlSession();

um=ss.getMapper(UserMapper.class);

}

@Afterpublic void tearDown() throwsException {

ss.commit();

ss.close();

}public voidtest() {

TestId ti= newTestId();

ti.setUsername("张张柳");

ti.setPassword("443221");//int i =ss.insert("com.dao.UserMapper.add",ti);

int i=um.add(ti);

System.out.println(i);

}public voidtest1(){int i = um.delete(401);

System.out.println(i);

}public voidtest2(){

TestId ti= newTestId();

ti.setId(new BigDecimal(441));

ti.setUsername("张张柳2");

ti.setPassword("443221");

um.update(ti);

}public voidtest3(){

TestId ti=um.select(441);

System.out.println(ti);

}

@Testpublic voidtes4(){

Map map = new HashMap();

map.put("username", "张%");

map.put("password", "%2%");

List list =um.selectlist(map);for(TestId ti:list){

System.out.println(ti);

}

}

}

Junit2.java如下:

packagecom.util;import static org.junit.Assert.*;importjava.math.BigDecimal;importjava.util.HashMap;importjava.util.List;importjava.util.Map;importorg.apache.ibatis.session.SqlSession;importorg.junit.After;importorg.junit.Before;importorg.junit.Test;importcom.dao.UserInfoMapper;importcom.dao.UserMapper;importcom.model.TestId;importcom.model.TestInfo;public classJunit2 {privateSqlSession ss;privateUserInfoMapper um;

@Beforepublic void setUp() throwsException {

ss=MybatisUtil.getSqlSession();

um=ss.getMapper(UserInfoMapper.class);

}

@Afterpublic void tearDown() throwsException {

ss.commit();

ss.close();

}

@Testpublic voidtest() {

List list =um.select();for(TestInfo ti :list){

System.out.println(ti);

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值