mybatis 实现增删改查
A就是一个普通的 maven项目,B项目新建的时候,必须继承的父项目。
比如:在B项目(增删改查的干活)中,引入A项目(提供jar包),
目录结构:
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>
数据库表内容