、环境
1.创建maven工程
2.导入相关依赖
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.36</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.2</version>
</dependency>
</dependencies>
3.在mysql数据库建表
DROP TABLE IF EXISTS `t_user`; CREATE TABLE `t_user` ( `id` int(30) NOT NULL AUTO_INCREMENT, `name` varchar(30) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, `pwd` varchar(30) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, `sex` varchar(30) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, `age` int(30) NULL DEFAULT NULL, PRIMARY KEY (`id`) USING BTREE ) ENGINE = InnoDB AUTO_INCREMENT = 7 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact; -- ---------------------------- -- Records of t_user -- ---------------------------- INSERT INTO `t_user` VALUES (1, '张三', '123', '男', 21); INSERT INTO `t_user` VALUES (2, '李四', '234', '男', 22); INSERT INTO `t_user` VALUES (3, '王五', '345', '男', 23); INSERT INTO `t_user` VALUES (4, '晶晶', '456', '女', 24); INSERT INTO `t_user` VALUES (5, '菁菁', '567', '女', 24); INSERT INTO `t_user` VALUES (6, '婧婧', '678', '女', 23); SET FOREIGN_KEY_CHECKS = 1;
二、处理基本配置
1、在资源目录下核心配置文件
<?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核心配置文件-->
<configuration>
<properties resource="user.properties"></properties>
<settings>
<!--设置mybatis输出日志-->
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>
<!-- 环境配置-->
<environments default="development">
<environment id="development">
<!-- 设置事务管理器类型为JDBC-->
<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>
<!-- 每一个Mapper.xml都要在mybatis核心配置文件中注册!-->
<mappers>
<mapper resource="mapper/UserMapper.xml"></mapper>
</mappers>
</configuration>
3.在java文件夹下创建三个同级包,并在utils工具包下创建获取sqlSession的工具类
3.1结构
3.2MybatisUtil工具类
package utils;
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;
public class MybatisUtil {
private static SqlSessionFactory sqlSessionFactory;
static {
try {
//获取资源
String resource = "mybatis-config.xml";
//加载资源
InputStream resourceAsStream = Resources.getResourceAsStream(resource);
//创建sqlSessionFactory实例
sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
} catch (IOException e) {
e.printStackTrace();
}
}
//定义获取SqlSession的方法
public static SqlSession getSqlsession(){
SqlSession sqlSession;
if (sqlSessionFactory!=null){
sqlSession = sqlSessionFactory.openSession();
}else {
sqlSession = null;
}
return sqlSession;
}
public static void main(String[] args) {
getSqlsession();
}
}
3.3运行main方法测试工具类:
三、编写
1.在pojo包下创建user实体类(数据库表单的映射)
package pojo;
public class User {
private Integer id;
private String name;
private String pwd;
private String sex;
private Integer age;
public User() {
}
public User(String name, String pwd, String sex, Integer age) {
this.name = name;
this.pwd = pwd;
this.sex = sex;
this.age = age;
}
public User(Integer id, String name, String pwd, String sex, Integer age) {
this.id = id;
this.name = name;
this.pwd = pwd;
this.sex = sex;
this.age = age;
}
public Integer getId() {
return id;
}
public void setId(Integer 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;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
2.在mapper包新建UserMapper接口并定义方法
package mapper;
import pojo.User;
import java.util.List;
public interface UserMapper {
//添加
public Integer addUser(User user);
//删除
public Integer delUserById(Integer id);
//修改
public Integer updateUser(User user);
//查询
public List<User> findAll();
//锁定条件查询
public List<User> findUserBySG(String sex,Integer age);
}
3.在resources目录下新建mapper包,包下新建一个名为UserMapper.xml的xml文件(映射文件与测试类结构与实际Java包结构一致)
<?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="mapper.UserMapper">
</mapper>
4.在核心配置文件注册接口的映射文件(给出映射文件的路径)
5.在映射文件写入sql语句(注意id与Mapper方法名对应)
<?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="mapper.UserMapper">
<insert id="addUser" parameterType="pojo.User">
insert into t_user (`name`,pwd,sex,age) values (#{name},#{pwd},#{sex},#{age})
</insert>
<delete id="delUserById" parameterType="java.lang.Integer">
delete from t_user where id=#{id}
</delete>
<update id="updateUser" parameterType="pojo.User">
update t_user set `name`=#{name},pwd=#{pwd},sex=#{sex},age=#{age} where id=#{id}
</update>
<select id="findAll" resultType="pojo.User">
select * from t_user
</select>
<select id="findUserBySG" resultType="pojo.User">
select * from t_user where sex=#{arg0}and age=#{arg1}
</select>
</mapper>
6.测试
package mapper;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import pojo.User;
import utils.MybatisUtil;
public class UserMapperTest {
@Test
public void addUserTest(){
SqlSession sqlsession = MybatisUtil.getSqlsession();
UserMapper mapper = sqlsession.getMapper(UserMapper.class);
User user = new User("静静","000","女",21);
mapper.addUser(user);
sqlsession.commit();
sqlsession.close();
}
@Test
public void delUserByIdTest(){
SqlSession sqlsession = MybatisUtil.getSqlsession();
sqlsession.getMapper(UserMapper.class).delUserById(7);
sqlsession.commit();
sqlsession.close();
}
@Test
public void updateUserTest(){
SqlSession sqlsession = MybatisUtil.getSqlsession();
User user = new User(2,"麻子","123","男",4);
sqlsession.getMapper(UserMapper.class).updateUser(user);
sqlsession.commit();
sqlsession.close();
}
@Test
public void findAllTest(){
SqlSession sqlsession = MybatisUtil.getSqlsession();
sqlsession.getMapper(UserMapper.class).findAll();
sqlsession.close();
}
@Test
public void findUserBySGTest(){
SqlSession sqlsession = MybatisUtil.getSqlsession();
sqlsession.getMapper(UserMapper.class).findUserBySG("女",24);
sqlsession.close();
}
}
7.整体工程结构
?
|