mybatis配置文件 dbConfig.properties driver=com.mysql.jdbc.Driverurl=jdbc:mysql://localhost:3306/dbusername=rootpassword=123 java代码 package com.amiu.mybatisTest;import java.io.IOException;import java.io.InputStream;import java.util.L
dbConfig.properties
driver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/db username=root password=123java代码
package com.amiu.mybatisTest;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
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.amiu.TestBean;
public class MyBatisTest {
//路径请自行修改
String resource = "com/amiu/mybatisTest/mybatisConfig.xml";
@Test
public void findById(){
InputStream is = null;
SqlSession sqlSession = null;
try {
is = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory =
new SqlSessionFactoryBuilder().build(is);
sqlSession = sqlSessionFactory.openSession();
TestBean bean = sqlSession.selectOne("test.findById",1);
System.out.println(bean.getId());
System.out.println(bean.getName());
System.out.println(bean.getPassword());
System.out.println(bean.getPhone());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Test
public void findByName(){
InputStream is = null;
SqlSession sqlSession=null;
try {
is = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory =
new SqlSessionFactoryBuilder().build(is);
sqlSession = sqlSessionFactory.openSession();
List
beansList = sqlSession.selectList("test.findByName","hellomini");
for (TestBean bean : beansList) {
System.out.println(bean.getId());
System.out.println(bean.getName());
System.out.println(bean.getPassword());
System.out.println(bean.getPhone());
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Test
public void insert(){
InputStream is = null;
SqlSession sqlSession = null;
try {
is = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory =
new SqlSessionFactoryBuilder().build(is);
sqlSession = sqlSessionFactory.openSession();
TestBean test = new TestBean();
//test.setId(4);
test.setName("beanName");
test.setPassword("beanPwd1");
test.setPhone(130000001);
sqlSession.insert("test.insert",test);
sqlSession.commit();
System.out.println(test.getId());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Test
public void deleteById(){
InputStream is = null;
SqlSession sqlSession = null;
try {
is = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory =
new SqlSessionFactoryBuilder().build(is);
sqlSession = sqlSessionFactory.openSession();
sqlSession.delete("test.deleteById",5);
sqlSession.commit();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Test
public void updateById(){
InputStream is = null;
SqlSession sqlSession=null;
try {
is = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory =
new SqlSessionFactoryBuilder().build(is);
sqlSession = sqlSessionFactory.openSession();
TestBean bean = new TestBean();
bean.setId(4);
bean.setName("updateName");
sqlSession.update("test.updateById",bean);
sqlSession.commit();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
bean
package com.amiu;
import java.io.Serializable;
public class TestBean implements Serializable{
private static final long serialVersionUID = 1L;
long id;
String name;
String password;
long phone;
public TestBean(){}
public TestBean(long id,String name,String password,long phone){
this.id = id;
this.name = name;
this.password = password;
this.phone = phone;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public long getPhone() {
return phone;
}
public void setPhone(long phone) {
this.phone = phone;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return id+":("+name+","+password+","+phone+")";
}
}
