Mybatis的介绍以及基础开发工程搭建步骤。
这个问题的解释没有比官网更权威的了,直接搜索mybatis,或者通过github搜索。
github地址:https://github.com/mybatis/mybatis-3
根据官网进行工程的搭建。
工具:IDEA+Maven
File->Settings->Build,Execution,Depoyment->Build Tools->Maven
项目创建好了。
<!-- mybatis核心依赖 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.5</version>
</dependency>
<!-- mysql版本对应的数据库连接驱动包 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.23</version>
</dependency>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"https://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>
<mapper resource="org/mybatis/example/BlogMapper.xml"/>
</mappers>
</configuration>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"https://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.cj.jdbc.Driver"/>
<property name="url" value="XXXXXXXXXXXX"/>
<property name="username" value="XXXXX"/>
<property name="password" value="XXXXXXX"/>
</dataSource>
</environment>
</environments>
</configuration>
package com.mybatisDemo.pojo;
public class EmpStaff {
private String staffId;
private String staffName;
public String getStaffId() {
return staffId;
}
public void setStaffId(String staffId) {
this.staffId = staffId;
}
public String getStaffName() {
return staffName;
}
public void setStaffName(String staffName) {
this.staffName = staffName;
}
@Override
public String toString() {
return "EmpStaff{" +
"staffId='" + staffId + '\'' +
", staffName='" + staffName + '\'' +
'}';
}
}
package com.mybatisDemo.mapper;
import com.mybatisDemo.pojo.EmpStaff;
public interface EmpStaffMapper {
/**添加mapper中对应操作的方法
* 1、方法名要和mapper中对应的操作的节点的id要一致
* 2、返回类型要和mapper中对应的操作的节点的resultTyoe一致
* 3、mapper中对应的操作的节点的参数要在方法的参数中声明
**/
EmpStaff selectStaff(String staffId);
}
mapper.xml中的namespace必须要和接口的全限定名一致
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.mybatisDemo.mapper.EmpStaffMapper">
<select id="selectStaff" resultType="com.mybatisDemo.pojo.EmpStaff">
select * from emp_staff where staffId = #{staffId}
</select>
</mapper>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"https://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.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://rm-2zed50a7ka8y9ov9f4o.mysql.rds.aliyuncs.com:3306/mybatisdemo"/>
<property name="username" value="root"/>
<property name="password" value="Jian339661"/>
</dataSource>
</environment>
</environments>
<mappers>
<!-- <mapper resource="EmpStaffMapper.xml"/>-->
<mapper class="com.mybatisDemo.mapper.EmpStaffMapper" ></mapper>
</mappers>
</configuration>
@Test
public void test02() throws IOException {
//通过xml的配置文件 得到一个SqlSessionFactory对象实例
String resource = "mybatis.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
try (SqlSession session = sqlSessionFactory.openSession()) {
EmpStaffMapper mapper = session.getMapper(EmpStaffMapper.class);
EmpStaff empStaff = mapper.selectStaff("101");
System.out.println(empStaff);
}
}
修改mapper接口方法
package com.mybatisDemo.mapper;
import com.mybatisDemo.pojo.EmpStaff;
import org.apache.ibatis.annotations.Select;
public interface EmpStaffMapper {
@Select("select * from emp_staff where staffId = #{staffId}")
EmpStaff selectStaff(String staffId);
}
注意:注解可以和xml共用 但是不能同时存在方法对应的xml的id
增删改涉及到了数据库更改,所以需要进行session.commit()最后的提交才会修改数据库数据,在后边与spring整合之后这些操作就省去了,后边再说。
@Test
public void test04() throws IOException {
//通过xml的配置文件 得到一个SqlSessionFactory对象实例
String resource = "mybatis.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession session = sqlSessionFactory.openSession();
try {
EmpStaffMapper mapper = session.getMapper(EmpStaffMapper.class);
EmpStaff empStaff = new EmpStaff();
empStaff.setStaffId("102");
empStaff.setStaffName("小二");
int i = mapper.addStaff(empStaff);
session.commit();
System.out.println(empStaff);
}catch (Exception e){
session.rollback();
}finally {
session.close();
}
}
到这里整个mybatis工程算是搭建完成了!其实这些在官网上都说的很详细,可以多去研究一下官网。
(ps:更多学习交流以及资料可以关注我得公众号:阿剑笔记)