pojo层
package com.hnzj.pojo;
public class User {
private int id;
private String name;
private String pwd;
public User() {
}
public User(int id, String name, String pwd) {
this.id = id;
this.name = name;
this.pwd = pwd;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", pwd='" + pwd + '\'' +
'}';
}
}
dao层
package com.hnzj.dao;
import com.hnzj.pojo.User;
import java.util.List;
import java.util.Map;
public interface UserMapper {
//查询所有用户
List<User> getUserList();
//根据指定id查询用户
User getUserById(int id);
//增
int addUser(User user);
int addUser2(Map<String,Object> map);
//删
int deleteUser(int id);
//改
int updateUser(User user);
}
<?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="com.hnzj.dao.UserMapper">
<select id="getUserList" resultType="Userbieming">
select *
from mybatis.user
</select>
<select id="getUserById" parameterType="int" resultType="com.hnzj.pojo.User">
select *
from mybatis.user
where id=#{id}
</select>
<insert id="addUser" parameterType="com.hnzj.pojo.User">
insert into mybatis.user (id,name,pwd)
values (#{id},#{name},#{pwd})
</insert>
<insert id="addUser2" parameterType="map">
insert into mybatis.user (id,pwd)
values (#{userid},#{passsWord})
</insert>
<update id="updateUser" parameterType="com.hnzj.pojo.User">
update mybatis.user set name=#{name},pwd=#{pwd} where id=#{id}
</update>
<delete id="deleteUser" parameterType="int">
delete from mybatis.user
where id=#{id}
</delete>
</mapper>
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC
username=****
password=*****
<?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="db.properties"/>
<typeAliases>
<typeAlias type="com.hnzj.pojo.User" alias="Userbieming"></typeAlias>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/hnzj/dao/UserMapper.xml"/>
</mappers>
</configuration>
测试类
package com.hnzj.dao;
import com.hnzj.pojo.User;
import com.hnzj.util.MybatisUtis;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class UserDaoTest {
@Test
public void test01(){
//1、获取1SqlSession对象
SqlSession sqlSession = MybatisUtis.getSqlSession();
//2、得到Mapper
UserMapper userDao = sqlSession.getMapper(UserMapper.class);
List<User> userList = userDao.getUserList();
for (User user:userList){
System.out.println(user);
}
//关闭sqlSession
sqlSession.close();
}
@Test
public void test02(){
SqlSession sqlSession = MybatisUtis.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
User userById = mapper.getUserById(2);
System.out.println(userById);
}
@Test
public void test03(){
SqlSession sqlSession = MybatisUtis.getSqlSession();
Object mapper = sqlSession.getMapper(UserMapper.class);
((UserMapper) mapper).addUser(new User(4,"哈哈","12345"));
//提交事务,增删改都要提交
sqlSession.commit();
sqlSession.close();
}
@Test
public void test032(){
SqlSession sqlSession = MybatisUtis.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
Map<String, Object> map = new HashMap<>();
map.put("userid",7);
map.put("passsWord","2222333");
mapper.addUser2(map);
sqlSession.commit();
sqlSession.close();
}
@Test
public void test04(){
SqlSession sqlSession = MybatisUtis.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
mapper.updateUser(new User(4,"呵呵","123456"));
sqlSession.commit();
sqlSession.close();
}
@Test
public void test05(){
SqlSession sqlSession = MybatisUtis.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
mapper.deleteUser(2);
sqlSession.commit();
sqlSession.close();
}
}
util类
package com.hnzj.util;
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 java.io.IOException;
import java.io.InputStream;
//sqlSessionFactory ----》sqlSession
public class MybatisUtis {
private static SqlSessionFactory sqlSessionFactory;
static{
try {
//使用mybatis获取sqlSessionFactory对象
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
//既然有了 SqlSessionFactory,顾名思义,我们可以从中获得 SqlSession 的实例。
// SqlSession 提供了在数据库执行 SQL 命令所需的所有方法
public static SqlSession getSqlSession(){
return sqlSessionFactory.openSession();
}
}
数据库
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">
<parent>
<artifactId>Mybatis-Study</artifactId>
<groupId>com.hnzj.javaee</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>mybatis-01</artifactId>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
<scope>test</scope>
</dependency>
</dependencies>
<properties>
<maven.compiler.source>15</maven.compiler.source>
<maven.compiler.target>15</maven.compiler.target>
</properties>
<!--在build中配置resources,来防止我们资源导出失败的问题-->
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>
</project>