mybatis的增删改查
mybatis功能强大在映射语句,配置映射器XML文件相对简单。下面是mybats的增删改查
mybatis-config.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">
<!-- XML 配置文件包含对 MyBatis 系统的核心设置 -->
<configuration>
<!-- 指定 MyBatis 所用日志的具体实现 -->
<settings>
<setting name="logImpl" value="LOG4J"/>
</settings>
<typeAliases>
<package name="com.rookie.bigdata.domain"/>
</typeAliases>
<environments default="mysql">
<!-- 环境配置,即连接的数据库。 -->
<environment id="mysql">
<!-- 指定事务管理类型,type="JDBC"指直接简单使用了JDBC的提交和回滚设置 -->
<transactionManager type="JDBC"/>
<!-- dataSource指数据源配置,POOLED是JDBC连接对象的数据源连接池的实现。 -->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://192.168.47.151:3306/mybatis"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
<!-- mappers告诉了MyBatis去哪里找持久化类的映射文件 -->
<mappers>
<mapper resource="mapper/UserMapper.xml"/>
</mappers>
</configuration>
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">
<!-- namespace指用户自定义的命名空间。 -->
<mapper namespace="com.rookie.bigdata.mapper.UserMapper">
<!--
id="save"是唯一的标示符
parameterType属性指明插入时使用的参数类型
useGeneratedKeys="true"表示使用数据库的自动增长策略
-->
<insert id="saveUser" parameterType="com.rookie.bigdata.domain.User" useGeneratedKeys="true">
INSERT INTO T_USER(name,sex,age)
VALUES(#{name},#{sex},#{age})
</insert>
<!-- select操作
parameterType="int"表示该查询语句需要一个int类型的参数
resultType="user"表示返回的是一个user对象 -->
<select id="selectUser" parameterType="int" resultType="user">
SELECT * FROM T_USER WHERE id = #{id}
</select>
<select id="selectAll" resultType="user">
SELECT * FROM T_USER
</select>
<!-- update操作
parameterType="user"表示该更新语句需要一个user对象作为参数-->
<update id="updateUser" parameterType="user">
UPDATE T_USER
SET name = #{name},sex = #{sex},age = #{age}
WHERE id = #{id}
</update>
<!-- delete操作 parameterType="int"表示该查询语句需要一个int类型的参数-->
<delete id="DeleteUser" parameterType="int">
DELETE FROM TB_USER WHERE id = #{id}
</delete>
</mapper>
UserMapper.java
package com.rookie.bigdata.mapper;
import com.rookie.bigdata.domain.User;
import java.util.List;
/**
* Created by dell on 2019/6/17.
*/
public interface UserMapper {
void saveUser(User user);
User selectUser(Integer id);
void updateUser(User user);
void DeleteUser(Integer id);
List<User> selectAll();
}
SqlSessionFactoryUtil.java
package com.rookie.bigdata.factory;
import java.io.InputStream;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
public class SqlSessionFactoryUtil {
private static SqlSessionFactory sqlSessionFactory = null;
// 初始化创建SqlSessionFactory对象
static{
try {
// 读取mybatis-config.xml文件
InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
sqlSessionFactory = new SqlSessionFactoryBuilder()
.build(inputStream);
} catch (Exception e) {
e.printStackTrace();
}
}
// 获取SqlSession对象的静态方法
public static SqlSession getSqlSession(){
return sqlSessionFactory.openSession();
}
// 获取SqlSessionFactory的静态方法
public static SqlSessionFactory getSqlSessionFactory() {
return sqlSessionFactory;
}
}
测试增删改查
package com.rookie.bigdata.mapper;
import com.rookie.bigdata.domain.User;
import com.rookie.bigdata.factory.SqlSessionFactoryUtil;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import java.util.List;
import static org.junit.Assert.*;
/**
* Created by dell on 2019/6/17.
*/
public class UserMapperTest {
@Test
public void saveUser() throws Exception {
SqlSession sqlSession = SqlSessionFactoryUtil.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
mapper.saveUser(new User("张三", "男", 26));
sqlSession.commit();
sqlSession.close();
//sqlSession.commit();
}
@Test
public void selectUser() throws Exception {
SqlSession sqlSession = SqlSessionFactoryUtil.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
User user = mapper.selectUser(1);
System.out.println(user);
sqlSession.commit();
sqlSession.close();
}
@Test
public void updateUser() throws Exception {
SqlSession sqlSession = SqlSessionFactoryUtil.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
User user = new User("zhangsan","女",70);
user.setId(1);
mapper.updateUser(user);
sqlSession.commit();
sqlSession.close();
}
@Test
public void deleteUser() throws Exception {
SqlSession sqlSession = SqlSessionFactoryUtil.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
mapper.DeleteUser(1);
sqlSession.commit();
sqlSession.close();
}
@Test
public void selectAll() throws Exception {
SqlSession sqlSession = SqlSessionFactoryUtil.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
List<User> userList = mapper.selectAll();
for (User user : userList) {
System.out.println(user);
}
sqlSession.commit();
sqlSession.close();
}
}