一、mybaitis的增刪改查簡單操作
阿新 • • 發佈:2018-11-11
1、匯入jar包
mysql-connector-java-5.1.7-bin.jar
mybatis-3.2.7.jar
2、db.propertiese
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test?characterEncoding=utf8
jdbc.username=root
jdbc.password=123456
3、sqlMapperConfig.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> <properties resource="config/db.properties"> <!-- <property name="" value=""/> --> </properties> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="${jdbc.driver}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </dataSource> </environment> </environments> <mappers> <mapper resource="zml/conf/UserMapper.xml"/> </mappers> </configuration>
4、實體類
package zml.pojo; import java.util.Date; public class User { private int id; private String username; private Date birthday; private int sex; private String address; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public int getSex() { return sex; } public void setSex(int sex) { this.sex = sex; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } @Override public String toString() { return "User [id=" + id + ", username=" + username + ", birthday=" + birthday + ", sex=" + sex + ", address=" + address + "]"; } }
5、操作:
package zml.Test; import java.io.IOException; import java.io.InputStream; import java.util.Date; 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.Before; import org.junit.Test; import zml.pojo.User; public class MybatisFrist { private SqlSessionFactory sqlF; @Before public void createSqlsessionFactory() throws IOException{ String resource = "config/SqlMapperConfig.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); sqlF = new SqlSessionFactoryBuilder().build(inputStream); } @Test public void TestGeyById(){ SqlSession sqlSession = null; User user = null; try{ sqlSession= sqlF.openSession(); user = sqlSession.selectOne("user.getById",1); }catch(Exception e){ e.printStackTrace(); }finally{ sqlSession.close(); } System.out.println(user); } @Test public void TestLike(){ SqlSession sqlSession = null; List<User> userList = null; try{ sqlSession= sqlF.openSession(); //userList = sqlSession.selectList("user.getByLikeUsername", "%小明"); userList = sqlSession.selectList("user.getByLikeUsername2", "小明"); }catch(Exception e){ e.printStackTrace(); }finally{ sqlSession.close(); } System.out.println(userList); } @Test public void TestUpdate(){ SqlSession sqlSession = null; try{ sqlSession= sqlF.openSession(); User user = new User(); user.setId(10); user.setBirthday(new Date()); user.setUsername("zzml"); sqlSession.update("user.updateUser", user); sqlSession.commit(); }catch(Exception e){ e.printStackTrace(); }finally{ sqlSession.close(); } } @Test public void TestDel(){ SqlSession sqlSession = null; try{ sqlSession= sqlF.openSession(); sqlSession.delete("user.deleteUser", 26); sqlSession.commit(); }catch(Exception e){ e.printStackTrace(); }finally{ sqlSession.close(); } } @Test public void TestAdd(){ SqlSession sqlSession = null; User user = new User(); try{ sqlSession= sqlF.openSession(); //DOTO 表結構中 id為自增主鍵 user.setBirthday(new Date()); user.setUsername("zengmeiling2"); //sqlSession.insert("user.add",user); sqlSession.insert("user.add2",user); sqlSession.commit(); }catch(Exception e){ e.printStackTrace(); }finally{ sqlSession.close(); } System.out.println("====ID:==="+user.getId()); } }