Mybatis入门案例
1、首先在pom.xml加入依赖
<!--第一个是mybatis包-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.1</version>
</dependency>
<!--第二个是myspql驱动jar包-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.49</version>
</dependency>
<!--build里面添加如下,***@3***-->
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
2、在com.bjpowernode/下创建domain包,domain包下创建按实体类Student.java
private Integer id;
private String name;
private String email;
private Integer age;
然后get和set方法,tostring方便输出
3、在com.bjpowernode/下创建dao包,dao包下创建StudentDao接口,接口内写入要操作的方法名称:
public List<Student> selectStudents();
4、在com.bjpowernode/dao/下创建sql的映射文件StudentDao.xml,映射文件名称和接口名称一样
<?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="com.bjpowernode.dao.StudentDao">
<select id="selectStudents" resultType="com.bjpowernode.domain.Student">
select id,name ,email,age from student order by id
</select>
</mapper>
namespace的值要和接口的全限定名称一致
id的值要和接口的方法名称一致
resultType的值要和实体类的全限定名称一致
查询用select标签,更新用update标签,还有删除delete和insert
5、然后再main下创建resources,右键resources ,选择 Mark Driectory as----->Resources root------>创建Mybatis.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>
<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/test"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/bjpowernode/dao/StudentDao.xml"/>
</mappers>
</configuration>
注意
mapper标签下的resource值是从更路径下开始的,即编译后的(target/class)开始的,值为StudentDao.xml的全限定名称,编译以后在target/class、com/bjpowernode/dao找不到StudentDao.xml,需要将pom.xml的@3加入即可
6、然后再com.bjpowernode下创建MyApp.java测试:
package com.bjpowernode;
import com.bjpowernode.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;
import java.util.List;
public class MyApp {
public static void main(String[] args) throws IOException {
String config="mybatis.xml";
InputStream in=Resources.getResourceAsStream(config);
SqlSessionFactoryBuilder builder=new SqlSessionFactoryBuilder();
SqlSessionFactory factory=builder.build(in);
SqlSession sqlSession=factory.openSession();
String sqlId="com.bjpowernode.dao.StudentDao"+"."+"selectStudents";
List<Student> studentList=sqlSession.selectList(sqlId);
studentList.forEach( stu-> System.out.println(stu));
sqlSession.close();
}
}
然后运行即可
各模块代码如下:
student.java(实体类)
package com.bjpowernode.domain;
public class Student {
private Integer id;
private String name;
private String email;
private Integer age;
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 String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", email='" + email + '\'' +
", age=" + age +
'}';
}
}
StudentDao.java(dao接口代码):
package com.bjpowernode.dao;
import com.bjpowernode.domain.Student;
import java.util.List;
public interface StudentDao {
public List<Student> selectStudents();
}
StudentDao.xml(sql映射文件):
<?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="com.bjpowernode.dao.StudentDao">
<select id="selectStudents" resultType="com.bjpowernode.domain.Student">
select id,name ,email,age from student order by id
</select>
</mapper>
mybatis.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>
<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/test"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/bjpowernode/dao/StudentDao.xml"/>
</mappers>
</configuration>
MyApp.java(测试类):
package com.bjpowernode;
import com.bjpowernode.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;
import java.util.List;
public class MyApp {
public static void main(String[] args) throws IOException {
String config="mybatis.xml";
InputStream in=Resources.getResourceAsStream(config);
SqlSessionFactoryBuilder builder=new SqlSessionFactoryBuilder();
SqlSessionFactory factory=builder.build(in);
SqlSession sqlSession=factory.openSession();
String sqlId="com.bjpowernode.dao.StudentDao"+"."+"selectStudents";
List<Student> studentList=sqlSession.selectList(sqlId);
studentList.forEach( stu-> System.out.println(stu));
sqlSession.close();
}
}
插入数据步骤:
dao接口写入方法接口(StudentDao.java):在以上代码添加代码
//student,表示要插入数据库的对象
//返回值,int 表示执行insert操作后,影响数据库的行数
public int insertStudent(Student student);
sql映射文件(StudentDao.java)添加sql语句:
<insert id="insertStudent">
insert into student values(#{id},#{name},#{email},#{age})
</insert>
创建test测试类TestMybatis:
@Test
public void testInsert() throws IOException {
String config="mybatis.xml";
InputStream in= Resources.getResourceAsStream(config);
SqlSessionFactoryBuilder builder=new SqlSessionFactoryBuilder();
SqlSessionFactory factory=builder.build(in);
SqlSession sqlSession=factory.openSession();
String sqlId="com.bjpowernode.dao.StudentDao"+"."+"insertStudent";
Student student=new Student();
student.setId(1003);
student.setName("张飞");
student.setEmail("zhangfei@123");
student.setAge(22);
int nums=sqlSession.insert(sqlId,student);
//mybatis默认是不自动提交事务,需要在insert,update,delete后手工提交事务
sqlSession.commit();
System.out.println("插入成功"+nums);
sqlSession.close();
}
}
视频资料:https://www.bilibili.com/video/BV185411s7Ry?p=6