(如有错漏,望大神指正,依赖包版本不同有可能出现不同问题)
关于mybatis
1,传统的JDBC代码都是写死的,存在硬编码问题
2,修改参数的方式繁琐(set get)
mybatis是什么(概括,详情见MyBatis中文网)
1,是一款持久层框架
2,简化了JDBC代码
3,可以通过xml和注解来配置和映射
MyBatis
1,连接数据库,创建mybatis数据库(这里用到MySQL)
Navicat可视化连接数据库,mysql初始用户名:root;密码:123456(可自行修改)
创建mybatis数据库;
CREATE DATABASE mybatis;
2,创建要查询的MySQL数据表
在mybatis库中创建Student数据表
--创建表
create TABLE Student(
sid int primary key auto_increment,
sname varchar(64) not null,
note varchar(200) not null,
);
--添加元素
insert int Student(sname,note)values('颜回' ,'一箪⻝,一瓢饮,在陋巷,人不堪其忧,回也不改其乐');
INSERT INTO student (sname, note)VALUES('闵损','二十四孝之芦衣顺母;母在一子寒,母去三子单'
);
INSERT INTO student (sname, note)
VALUES
(
'冉伯牛',
'不幸患麻⻛病,端正正派,善于待人接物'
);
INSERT INTO student (sname, note)
VALUES
(
'仲弓',
'冉雍出身贫贱,但气量宽宏,沉默厚重'
);
INSERT INTO student (sname, note)
VALUES
(
'冉求',
'冉求个性谨慎,做事畏缩;力不足者,中道而废。今女画'
);
INSERT INTO student (sname, note)
VALUES
(
'子路',
'生性豪爽,为人耿直,有勇力才艺,闻过则喜'
);
INSERT INTO student (sname, note)
VALUES
(
'子贡',
'子贡与子路一文一武,犹如孔子的左右手,尤善经商,首富,但不是君子,因为君子
不器'
);
INSERT INTO student (sname, note)
VALUES
(
'子我',
'口⻮伶俐,能说善辩,被孔子骂朽木不可雕也'
);
INSERT INTO student (sname, note)
VALUES
(
'子游',
'割鸡焉用牛刀典故主⻆,割鸡焉用牛刀;君子学道则爱人,小人学道则易使也'
);
INSERT INTO student (sname, note)
VALUES
(
'子夏',
'学而优则仕提出者;巧笑倩兮,美目盼兮,素以为绚兮’,何谓也?”子曰:“绘事后
素'
);
--查看表
select * from Student;
3,打开idea,创建maven项目
1, 配置pom文件(junit,log2j,mysal-connector-java,mybatis)
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.12</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.46</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.2</version>
</dependency>
2,新建配置文件mybatis-config.xml(mybatis中文网可查到相关代码,拷贝加以修改)
<?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/mybatis"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
</configuration>
4,接口StudentMapper
5,实体类Student
package org.example;
public class Student {
private Integer sid;
private String sname;
private String note;
public Student(Integer sid, String sname, String note) {
this.sid = sid;
this.sname = sname;
this.note = note;
}
public Student() {
}
@Override
public String toString() {
return "Student{" +
"sid=" + sid +
", sname='" + sname + '\'' +
", note='" + note + '\'' +
'}';
}
public Integer getSid() {
return sid;
}
public void setSid(Integer sid) {
this.sid = sid;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public String getNote() {
return note;
}
public void setNote(String note) {
this.note = note;
}
}
6,映射文件StudentMapper.xml(mybatis中文网可查到相关代码,拷贝加以修改)
<?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="org.example.StudentMapper">
<select id="selectStudent" resultType="org.example.Student">
select * from Student where sid = #{id}
</select>
</mapper>
7,再次修改配置文件,添加如下(其实就直接修改cop的代码引号里的内容)
<mappers>
<mapper resource="StudentMapper.xml"/>
</mappers>
在接口中增加方法
8,写测试文件
package org.example;
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;
/**
* Hello world!
*
*/
public class App
{
public static void main( String[] args ) throws IOException
{
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
try(SqlSession session = sqlSessionFactory.openSession()){
Student stu = (Student)session.selectOne("org.example.StudentMapper.selectStudent",2);
System.out.println(stu);
}
}
}
9,测试结果