mybatis 實現增刪改查
阿新 • • 發佈:2020-08-25
目錄結構:
Dao
package test.Dao; import test.entity.User; import java.util.List; public interface UserDao { List<User> selectAll(); List<User> selectByName(String name); List<User> selectById(int id); int addUser(User user); int delete(int id); // 通過id修改學員資訊 int updateUser(User user); // 統計班級人數 int num(); }
User 實體類
import java.io.Serializable;
public class User implements Serializable {
private int id;
private String name;
private int sex ;
private int age ;
private String address;
private String tel;
private String email;
UserMapper.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="test.Dao.UserMapper"> <!-- 方法名,返回型別--> <select id="selectAll" resultType="test.entity.User"> -- 查詢語句,表名加標號 select * from `test` </select> <select id="selectByName" resultType="test.entity.User"> select * from `test` where name=#{name} </select> <select id="selectById" resultType="test.entity.User"> select * from `test` where id=#{id} </select> <insert id="addUser" parameterType="test.entity.User"> insert into `test`(id,name,sex,age,address,tel,email)value(#{id},#{name},#{age},#{sex},#{address},#{tel},#{email}) </insert> <!-- 錯誤的寫法 --> <!-- <update id="updateUser" parameterType="int">--> <!-- update `mabatis01` set uname="小哥" uage="18" where uid=#{uid}--> <!-- </update>--> <delete id="delete" parameterType="int"> delete from `test` where id=#{id} </delete> <!-- update `mabatis01` set uname=#{uname},uage=#{uage} 後面跟的引數,無雙引號 --> <!-- 註釋不要寫在sql 語句語句裡,否則當成sql語句解析。 --> <update id="updateUser" parameterType="test.entity.User"> update `test` set name=#{name},sex=#{sex},age=#{age},address=#{address},tel=#{tel},email=#{email} where id=#{id} </update> <select id="num" resultType="int"> select count(1) from `test` </select> </mapper>
jdbcConfig.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/Test
jdbc.username=root
jdbc.password=root
SqlSessionConfig.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> <!-- 加入這個,意味著要用el表示式--> <properties resource="jdbcConfig.properties"></properties> <!-- 資料庫的配置--> <environments default="mysql"> <environment id="mysql"> <transactionManager type="JDBC"></transactionManager> <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="test/Dao/UserMapper.xml"></mapper> <!-- <mapper class="guitar.Dao.UserMapper"></mapper>--> <!-- <package name="guitar.Dao"/>--> <!-- 原生路徑 --> <!-- <mapper resource="guitar/Dao/UserMapper.xml"></mapper>--> </mappers> </configuration>
package test;
import test.entity.User;
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 java.io.IOException;
import java.io.InputStream;
import java.util.List;
public class test {
@Test
public void h1() throws IOException {
InputStream is = Resources.getResourceAsStream("SqlSessionConfig.xml");
SqlSessionFactory sf = new SqlSessionFactoryBuilder().build(is);
SqlSession session = sf.openSession();
List<User> userList = session.selectList("test.Dao.UserMapper.selectAll");
for (User user:userList){
System.out.println(user);
}
session.commit();
session.close();
}
@Test
public void h2() throws IOException {
InputStream is = Resources.getResourceAsStream("SqlSessionConfig.xml");
SqlSessionFactoryBuilder sfb = new SqlSessionFactoryBuilder();
SqlSessionFactory sf = sfb.build(is);
SqlSession session = sf.openSession();
User user = new User();
user.setName("小李");
List<User> userList = session.selectList("test.Dao.UserMapper.selectByName",user);
for (User user2:userList){
System.out.println(user2);
}
session.commit();
session.close();
}
@Test
public void h3() throws IOException {
InputStream is = Resources.getResourceAsStream("SqlSessionConfig.xml");
SqlSessionFactoryBuilder sfb = new SqlSessionFactoryBuilder();
SqlSessionFactory sf = sfb.build(is);
SqlSession session = sf.openSession();
User user = new User();
user.setId(4);
List<User> userList = session.selectList("test.Dao.UserMapper.selectById",user);
for (User user2:userList){
System.out.println(user2);
}
session.commit();
session.close();
}
@Test
public void h4() throws IOException {
InputStream is = Resources.getResourceAsStream("SqlSessionConfig.xml");
SqlSessionFactoryBuilder sfb = new SqlSessionFactoryBuilder();
SqlSessionFactory sf = sfb.build(is);
SqlSession session = sf.openSession();
User user = new User();
user.setId(6);
user.setName("小豪");
user.setAge(22);
user.setSex(1);
user.setAddress("望城");
user.setTel("123456");
user.setEmail("qwerty");
// insert 第一個是呼叫 sql語句的方法,第二個是 插入的引數。
int num = session.insert("test.Dao.UserMapper.addUser",user);
System.out.println(num);
session.commit();
session.close();
}
@Test
public void h5() throws IOException {
InputStream is = Resources.getResourceAsStream("SqlSessionConfig.xml");
SqlSessionFactoryBuilder sfb = new SqlSessionFactoryBuilder();
SqlSessionFactory sf = sfb.build(is);
SqlSession session = sf.openSession();
// User user = new User();
// user.setId(3);
// insert 第一個是呼叫 sql語句的方法,第二個是 插入的引數。
int num = session.delete("test.Dao.UserMapper.delete",3);
System.out.println(num);
session.commit();
session.close();
}
@Test
public void h6() throws IOException {
InputStream is = Resources.getResourceAsStream("SqlSessionConfig.xml");
SqlSessionFactoryBuilder sfb = new SqlSessionFactoryBuilder();
SqlSessionFactory sf = sfb.build(is);
SqlSession session = sf.openSession();
User user = new User();
user.setId(2);
user.setName("小晴");
user.setAge(45);
user.setSex(0);
user.setAddress("城堡");
user.setTel("123456987");
user.setEmail("qwjhgfds");
// insert 第一個是呼叫 sql語句的方法,第二個是 插入的引數。
int num = session.update("test.Dao.UserMapper.updateUser",user);
System.out.println(num);
session.commit();
session.close();
}
@Test
public void h7() throws IOException {
InputStream is = Resources.getResourceAsStream("SqlSessionConfig.xml");
SqlSessionFactoryBuilder sfb = new SqlSessionFactoryBuilder();
SqlSessionFactory sf = sfb.build(is);
SqlSession session = sf.openSession();
// 查詢記錄數,用 selectOne
int num = session.selectOne("test.Dao.UserMapper.num");
System.out.println(num);
session.commit();
session.close();
}
}
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>test</groupId>
<artifactId>test</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.5</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.12</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
</dependency>
</dependencies>
</project>