第一个mybatis
简介
-
Maven仓库
-
Maven地址
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.3</version> </dependency>
1. 建立一个maven项目
2. 导入jdbc mybatis 和 junit
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.8</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
3. 在src/main/resources中建立mybatis-config.xml文件
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">
<configuration>
<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>
</mappers>
</configuration>
4.编写 SqlSessionFactory(固定写法)
官网
String resource = "org/mybatis/example/mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
自定义
package top.wmgx.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;
public class MybatisUtils {
private static String resource = "mybatis_config.xml"; //前面的配置文件名称
private static InputStream inputStream;
private static SqlSessionFactory sqlSessionFactory;
static {
try {
inputStream= Resources.getResourceAsStream(resource);
sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
}
catch (IOException e) {
e.printStackTrace();
}
}
public static SqlSession getSqlSession(){
return sqlSessionFactory.openSession();
}
}
5. 编写代码
实体类
实体类设置Getter Setter方法,其中的属性与数据库一致
Mapper实现接口(DAO)
建立mappe接口
和DAO一样
建立配置文件(同目录)
<?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接口-->
<mapper namespace="top.wmgx.mapper.UserMapper">
<!-- id为绑定接口的方法-->
<!-- resultType为返回类型的实体类型(写全路径)-->
<select id="getUserList" resultType="top.wmgx.pojo.User">
select * from user
</select>
</mapper>
namespace中文意思:命名空间,作用如下:
- namespace和子元素的id联合保证唯一 , 区别不同的mapper
- 绑定DAO接口
- namespace的命名必须跟某个接口同名
- 接口中的方法与映射文件中sql语句id应该一一对应
- namespace命名规则 : 包名+类名
在mybatis-config.xml中添加此接口的映射
<mappers>
<mapper resource="top/wmgx/mapper/UserMapper.xml"></mapper>
</mappers>
<!--
使用映射器接口实现类的完全限定类名
需要配置文件名称和接口名称一致,并且位于同一目录下
-->
<mappers>
<mapper class="top.wmgx.mapper.UserMapper"/>
</mappers>
<!--
将包内的映射器接口实现全部注册为映射器
但是需要配置文件名称和接口名称一致,并且位于同一目录下
-->
<mappers>
<package name="top.wmgx.mapper"/>
</mappers>
调用
package top.wmgx.mapper;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import top.wmgx.pojo.User;
import top.wmgx.util.MybatisUtils;
import java.util.List;
public class UserMapperTest {
@Test
public void test(){
//获取SqlSession对象
SqlSession sqlSession= (SqlSession) MybatisUtils.getSqlSession();
//执行SQL
UserMapper mapper =sqlSession.getMapper(UserMapper.class);
List<User> user=mapper.getUserList();
System.out.printf(user.toString());
sqlSession.close();
}
}
6. CRUD
6.1 namespace
namespace中的包名要与mapper(Dao)接口中的包名一致
6.2 select
选择查询:
- id:实现的方法
- resultType 返回值类型
- parameterType: 参数类型
-
编写接口
User getUserById(int id);
-
配置文件
<select id="getUser" resultType="top.wmgx.pojo.User" parameterType="int"> select * from user where id =#{id} </select>
6.3 Insert
- 需要用commit提交才可生效
-
编写接口
int addUser(User user);
-
配置文件
<insert id="addUser" parameterType="top.wmgx.pojo.User"> insert into user (id,name,pwd) values(#{id},#{name},#{pwd}) </insert>
-
测试
@Test public void insertUserTest(){ //获取SqlSession对象 SqlSession sqlSession= (SqlSession) MybatisUtils.getSqlSession(); UserMapper mapper =sqlSession.getMapper(UserMapper.class); User u = new User(4,"abc","12345"); mapper.addUser(u); sqlSession.commit(); sqlSession.close(); }
6.4 Update
- 需要用commit提交才可生效
-
编写接口
int updateUser(User user);
-
配置文件
<update id="updateUser" parameterType="top.wmgx.pojo.User"> update user set name= #{name},pwd=#{pwd} where id=#{id} </update>
-
测试
@Test public void insertUserTest(){ //获取SqlSession对象 SqlSession sqlSession= (SqlSession) MybatisUtils.getSqlSession(); UserMapper mapper =sqlSession.getMapper(UserMapper.class); User u = new User(4,"eas","54321"); mapper.addUser(u); sqlSession.commit(); sqlSession.close(); }
6.5 Delete
- 需要用commit提交才可生效
-
编写接口
int deleteUser(int id);
-
配置文件
<delete id="deleteUser" parameterType="int"> delete from user where id=#{id} </delete>
本文是原创文章,采用 CC BY-NC-ND 4.0 协议,完整转载请注明来自 程序员小航
评论
匿名评论
隐私政策
你无需删除空行,直接评论以获取最佳展示效果