学习能力强的建议查看官网学习:http://www.mybatis.org/mybatis-3/getting-started.html
创建Maven项目
Maven项目的创建查看此链接:https://blog.youkuaiyun.com/Kedongyu_/article/details/81365825
添加MyBatis相关类包
找到pom.xml,添加下列代码,保存:
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
创建config.xml文件
在Spring IoC容器配置文件同个目录下创建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>
<typeAliases>
<!-- Model类(表)别名 -->
</typeAliases>
<environments default="development">
<environment id="development">
<!-- 事务模式 -->
<transactionManager type="JDBC" />
<!-- 连接池方式 -->
<dataSource type="POOLED">
<!-- 驱动 -->
<property name="driver" value="oracle.jdbc.driver.OracleDriver" />
<!-- 数据库连接url -->
<property name="url" value="${url}" />
<!-- 账号 -->
<property name="username" value="${user}" />
<!-- 密码 -->
<property name="password" value="${password}" />
</dataSource>
</environment>
</environments>
<mappers>
<!-- 映射文件 -->
</mappers>
</configuration>
到这里,MyBatis的基本配置已经完成,下面是一个小例子。
1.修改配置文件,改为自己想要连接的数据库
2.创建数据库表
oracle版本:
--创建学生表
create table Student(
stuNo number,
stuName varchar2(16),
phone varchar2(11)
);
--序列
create sequence student_seq;
Mysql版本,使用Mysql的话,刚刚配置文件config.xml需要修改数据库驱动:
create table Student(
stuNo number AUTO_INCREMENT,
stuName varchar(16),
phone varchar(11)
);
3.创建对应的Model类:StudentModel.class,包路径为:com.example.model
public class StudentModel {
private int stuNo;
private String stuName;
private String phone;
public int getStuNo() {
return stuNo;
}
public void setStuNo(int stuNo) {
this.stuNo = stuNo;
}
public String getStuName() {
return stuName;
}
public void setStuName(String stuName) {
this.stuName = stuName;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
}
4.创建dao层接口,包路径为:com.example.dao
public interface IStudentDao {
//增加
public void create(StudentModel student) throws Exception;
//删除
public void delete(StudentModel student) throws Exception;
//修改
public void update(StudentModel student) throws Exception;
//查询-全部
public List<StudentModel> selectListByAll() throws Exception;
}
5.创建映射文件IStudentDaoMapper.xml,包路径:com.example.dao.mapper
其中:
namespace填映射的dao层接口(全路径,按Ctrl+鼠标轻触路径,路径出现下划线则说明是对的,下同)
id指的是对应的dao层接口里面定义的方法。
parameterType指的是方法传入参数类型
resultType指的是方法返回结果类型
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.dao.IStudentDao">
<insert id="create" parameterType="com.example.model.StudentModel">
insert into Student(StuNo,StuName,Phone)
values(Student_SEQ.nextval,#{stuName},#{phone})
</insert>
<update id="update" parameterType="com.example.model.StudentModel">
update Student set StuName=#{stuName}, Phone=#{phone}
where StuNo=#{stuNo}
</update>
<delete id="delete" parameterType="com.example.model.StudentModel">
delete from HT_Neighbourhood where StuNo=#{stuNo}
</delete>
<!-- 查询-全部 -->
<select id="selectListByAll" resultType="com.example.model.StudentModel">
select * from Student
</select>
</mapper>
6.在MyBatis配置文件config.xml指定映射文件。
<mappers>
<!-- 映射文件 -->
<mapper resource="com/example/dao/mapper/IStudentDaoMapper.xml"/>
</mappers>
6.创建测试文件:StudentDaoTest.class,包路径:com.example.test。
public class StudentDaoTest {
public static final void main(String[] args) throws Exception{
String resource = "config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession session = sqlSessionFactory.openSession();
IStudentDao studentDao=session.getMapper(IStudentDao.class);
createTest(studentDao);
session.commit();
updateTest(studentDao);
session.commit();
// deleteTest(studentDao);
// session.commit();
toListByAllTest(studentDao);
session.close();
}
public static void createTest(IStudentDao studentDao) throws Exception {
StudentModel student=new StudentModel();
student.setPhone("86110119120");
student.setStuName("某某1号");
studentDao.create(student);
System.out.println("添加成功!");
}
public static void updateTest(IStudentDao studentDao) throws Exception{
StudentModel student=new StudentModel();
student.setPhone("86110119120");
student.setStuName("某某2号");
student.setStuNo(2);
studentDao.create(student);
System.out.println("修改成功!");
}
public static void deleteTest(IStudentDao studentDao) throws Exception{
StudentModel student=new StudentModel();
student.setStuNo(2);
studentDao.delete(student);
System.out.println("添加成功!");
}
public static void toListByAllTest(IStudentDao studentDao) throws Exception{
List<StudentModel> list=studentDao.selectListByAll();
System.out.println("查询成功!");
for(StudentModel student:list) {
System.out.println(student.getStuNo()+" "+student.getStuName()+" "+student.getPhone());
}
}
}
6.以java Application方式执行测试文件。
控制台输出:
查看数据库: