Idea2019搭建Mybatis框架环境并完成一个简单的添加功能
使用Idea搭建Mybatis环境
1、通过Idea创建一个新的项目:选择Maven,小框框勾选,直接选择下一步输入自定义包名和项目名。
2、项目创建成功后,在左边的目录结构中选择pom.xml(一般项目创建后会自动打开),在xml文件中引入mybatis的jar和mysql的文件(在一开始添加时会报错,在idea右下边弹出的提示框中可以选择自动加载jar)。
自动导包操作如下
添加依赖操作如下
依赖如下:
<!-- mybatis核心包 -->
<dependencies>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.3.0</version>
</dependency>
<!-- mysql驱动包 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.29</version>
</dependency>
<!-- junit测试包 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
</dependencies>
添加成功后:
3.创建实体类(对应数据库的某张表)以及持久层类
Student.java实体类代码如下:
package com.yao.beans;
public class Student {
//定义属性
private Integer id; //可以进行为空的判断
private String name;
private int age;
private double score;
//定义构造方法
public Student() {
}
public Student(String name, int age, double score) {
this.name = name;
this.age = age;
this.score = score;
}
//定义setter和getter方法
public Integer getId() {
return id;
}
public void setId(Integer 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;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", score=" + score +
'}';
}
}
数据库表如下
持久层dao下创建一个接口以及一个实现类
IstudentDao.java接口代码如下
package com.yao.dao;
import com.yao.beans.Student;
/**
* @author yao
*/
public interface IStudentDao {
void insertStu(Student student);
}
StudentDaoImpl.java实现类代码如下
package com.yao.dao;
import com.yao.beans.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 IStudentDao {
public void insertStu(Student student) {
InputStream inputStream = null;
try {
//1.加载主配置文件
inputStream = Resources.getResourceAsStream("mybatis.xml");
//2.创建sqlSesionFactory对象
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//3.创建sqlSession对象
SqlSession sqlSession = sqlSessionFactory.openSession();
//4.相关操作
sqlSession.insert("insertStudent",student);
sqlSession.commit();
} catch (IOException e) {
e.printStackTrace();
}
}
}
4、添加相关配置文件(mybatis.xml)
1、mybatis的配置文件**(文件名:mybatis.xml)
放在resources包下
代码如下
<?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="mysqlEM">
<!--配置环境-->
<environment id="mysqlEM">
<!--数据库类型-->
<transactionManager type="JDBC"></transactionManager>
<!--配置连接池-->
<dataSource type="POOLED">
<!--配置连接数据库四要素-->
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/mytest"/>
<property name="username" value="root"/>
<property name="password" value="wi1104wi"/>
</dataSource>
</environment>
</environments>
<!--注册映射文件-->
<mappers>
<mapper resource="mapper/UserMapper.xml"></mapper>
</mappers>
</configuration>
5.添加UserMapper.xml配置文件(建议target包下classes包下新建一个包, mybatis.xml找映射是从/target/classes开始找的)
配置如下:
<?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="xxx">
<insert id="insertStudent" parameterType="com.yao.beans.Student">
insert into t_student(name,age,score) values(#{name},#{age},#{score})
</insert>
</mapper>
6.创建一个测试类
代码如下:
package test;
import com.yao.beans.Student;
import com.yao.dao.StudentDaoImpl;
import org.junit.Before;
import org.junit.Test;
public class MyTest {
private StudentDaoImpl dao;
@Before
public void before(){
dao = new StudentDaoImpl();
}
@Test
public void testInsert(){
Student student = new Student("laoliu",27,99.5);
dao.insertStu(student);
}
}
7.运行程序,得到如下结果就完美成功了啦
查询数据库得