1Maven依赖管理:
导入mybatis依赖:
1 2 3 4 5
| <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.10</version> </dependency>
|
mysql版本建议:
1 2 3 4 5
| <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.29</version> </dependency>
|
单元测试:
1 2 3 4 5 6
| <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13.1</version> <scope>test</scope> </dependency>
|
2复制粘贴mybatis配置文件:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| <?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="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/k2502?useUnicode=true&characterEncoding=utf-8"/> <property name="username" value="root"/> <property name="password" value="123"/> </dataSource> </environment> </environments> <mappers> <mapper resource="cn/k2502/dao/GradeMapper.xml"/> <mapper resource="cn/k2502/dao/StudentDaoMapper.xml"/> </mappers> </configuration>
|
请根据自己的实际情况填写SQL映射文件地址
3:编写SQL映射文件
以上文的第一个”cn/k2502/dao/GradeMapper.xml”为例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| <?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="cn.k2502.dao.GradeDao">
<select id="getAllGrade" resultType="cn.k2502.domain.Grade"> select * from grade </select> </mapper>
|
一个SQL映射文件需要映射两个部分:
1:命名空间namespace需要与接口全类名相同
所以我们需要一个名叫GradeDao的接口,实现如下:
(建议id与方法名相同)
1 2 3 4 5 6 7
| package cn.k2502.dao; import cn.k2502.domain.Grade; import java.util.List;
public interface GradeDao { List<Grade> getAllGrade(); }
|
2:结果集类型resultType需要与实体类domain相同
所以我们需要一个Grade的实体类,实现如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| package cn.k2502.domain;
public class Grade { private Integer gid; private String gname;
public Integer getGid() { return gid; }
public void setGid(Integer gid) { this.gid = gid; }
public String getGname() { return gname; }
public void setGname(String gname) { this.gname = gname; }
@Override public String toString() { final StringBuilder sb = new StringBuilder("Grade{"); sb.append("gid=").append(gid); sb.append(", gname='").append(gname).append('\''); sb.append('}'); return sb.toString(); } }
|
4:执行SQL
我们需要一个SQL执行的对象sqlSession,
这个对象从工厂中来,
这个工厂需要按照mybatis-config.xml文件建造,
最后需要一个工厂建造者。
那我们就翻过来编写代码,以GradeDaoImpl为例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| package cn.k2502.dao.impl;
import cn.k2502.domain.Grade; 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; import java.util.List;
public class GradeDaoImpl { public List<Grade> getAllGrade(){ List<Grade> list = null; try { SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder(); InputStream resourceAsStream = Resources.getResourceAsStream("mybatis-config.xml"); SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(resourceAsStream); SqlSession sqlSession = sqlSessionFactory.openSession(); list = sqlSession.selectList("cn.k2502.dao.GradeDao.getAllGrade"); sqlSession.close(); } catch (IOException e) { e.printStackTrace(); } return list; } }
|
5:一些有帮助的插件
如:mybatisX(他会显示一个小鸟的图标)
如: Alibaba Cloud AI Coding Assistant (阿里云智能编码插件,可以快速查找别人写在github上的参考代码)
插件安装方式自行百度。