今天開始學了mybatis,之前就聽說這個框架,所以這次就來嘗試一下。
MyBatis 是一款優秀的持久層框架,它支持定製化 SQL、存儲過程以及高級映射。MyBatis 避免了幾乎所有的 JDBC 代碼和手動設置參數以及獲取結果集。MyBatis 可以使用簡單的 XML 或註解來配置和映射原生類型、接口和 Java 的 POJO(Plain Old Java Objects,普通老式 Java 對象)爲數據庫中的記錄。
這篇文章主要講述一個最最最基本的mybatis程序,然後再一步步的修改。
主要內容如下:
1、創建maven工程,導入jar包
2、創建數據文件
3、創建實體類
4、創建dao接口
5、創建mapper文件
6、創建mybatis主配置文件
7、創建接口的實現類
8、進行測試
一晚上花了2個小時就從網上找了個教程,寫了一個demo文件,遇到了一些小錯誤,但是都解決了!從上面一步一步的介紹:
第一步:創建maven工程,導入jar包
這個步驟還是非常簡單的,再IEDA中建立一個webapp的工程即可,然後建立響應的文件夾,再pom文件中導入相應的jar:
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.0.4.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.62</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.12</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.13</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-validator -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>6.1.0.Final</version>
</dependency>
<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
</dependencies>
這裏面有的jar在這個項目中能用到,有的用不了,反正這些jar包都是常用的多導入些也沒關係,在我的電腦上,maven不能用,自己搞了好久才能用。
第二步:創建數據庫
這一步需要也是很簡單,老生常談的問題。
create database learnmybatis;
CREATE TABLE `t_student` (
`id` INT NOT NULL AUTO_INCREMENT,
`name` VARCHAR(20) NULL,
`age` INT NULL,
`score` DOUBLE NULL,
PRIMARY KEY (`id`));
第三步:創建實體類
package domain;
public class Student {
private int id;
private String name;
private int age;
private double score;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
//省略getter和setter
}
第四步:創建dao接口
這個步驟和之前創建的dao接口一直,就是定義查詢數據的方法
package dao;
import domain.Student;
public interface StudentDao {
public void insertStudent(Student student);
}
第五步:創建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="zhiguo98">
<!--parameterType可省略-->
<insert id="insertStudent" parameterType="domain.Student">
INSERT INTO t_student(name,age,score) VALUES (#{name},#{age},#{score})
</insert>
</mapper>
注意點:
namespace:命名空間,可以隨便命名
insert 對應的是個插入的數據庫語句
id是這個插入語句的唯一代號,不能重複
parameterType是參數類型,可加可不加
下面就是插入語句 注意每個參數的一致性,最好是數據庫和實體類保持一致,要不然指不定哪會出錯
第六步:創建主配置文件
這個文件是非常重要的,是連接數據庫的一些參數和mapper文件的位置
<?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.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/learnmybatis?serverTimezone=UTC"/>
<property name="username" value="root"/>
<property name="password" value="yanzhiguo140710"/>
</dataSource>
</environment>
</environments>
<mappers>
<!--註冊映射文件-->
<mapper resource="dao/StudentMapper.xml"/>
</mappers>
</configuration>
<mappers>
<!--註冊映射文件-->
<mapper resource="dao/StudentMapper.xml"/>
</mappers>
在主配置文件中配置mapper文件的位置!!!
第七步:創建接口的實現類
實現之前寫的dao接口,實現裏面的方法
package dao.impl;
import dao.StudentDao;
import domain.Student;
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 StudentDaoImpl implements StudentDao {
private SqlSession sqlSession;
@Override
public void insertStudent(Student student) {
try {
//讀取主配置文件
InputStream input = Resources.getResourceAsStream("mybatis.xml");
//創建SqlSessionFactory對象
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(input);
//創建SqlSession對象
sqlSession = sessionFactory.openSession();
//新增數據操作
sqlSession.insert("insertStudent", student);
//提交SqlSession
sqlSession.commit();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (sqlSession != null) {
sqlSession.close();
}
}
}
}
上面代碼的註釋部分是mybatis基礎的下層代碼查詢流程。。。
第八步:進行測試
這一步需要導入junit的jar,實現測試
package test;
import dao.StudentDao;
import dao.impl.StudentDaoImpl;
import domain.Student;
import org.junit.jupiter.api.Test;
public class StudentTest01 {
@Test
public void insertStudent(){
StudentDao studentDao = new StudentDaoImpl();
Student student = new Student();
student.setName("aaa");
student.setAge(12);
student.setScore(99.0);
studentDao.insertStudent(student);
}
}
結果:
我主要在數據庫的連接中出了一點點小小的問題,版本問題,把驅動改了就行了,還有一個時區的問題 。總體來說還算順利,明天接着學!!!