导入jar包
【mybatis】
【oracle】
生成数据库
1、添加Mybatis的配置文件mybatis-config.xml
在src目录下创建一个mybatis-config.xml文件,如下图所示:
/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、定义表所对应的实体类,如下图所示:
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映射文件
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的类,来进行测试
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);
}
}
}