mybatis官方文档
https://mybatis.org/mybatis-3/zh/getting-started.html
1.创建maven项目,导入mybatisey依赖和mysql依赖
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.21</version>
</dependency>
2.Java包下面创建两个包,实体包daomain以及实体接口包dao
daomain中书写JavaBean,类名叫Student.包括成员变量、getter及setter方法、toString方法
dao中接口定义一个查询方法
public interface StudentDao {
public List selectStudents();
}
3.在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="dao.StudentDao">
<select id="selectStudents" resultType="domain.Student">
select id,name,email,age from student order by id
</select>
</mapper>
对如上内容的解释:mapper的映射命名空间是接口的全限定名称
selsect id是接口中方法的名称、返回类型是实体类中类名的全限定名称
4.在main包下创建一个资源文件,命名为researse 在里面写主配置文件
<?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://localhost:3306/ssm"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="dao/StudentDao.xml"/>
</mappers>
</configuration>
对以上内容进行解释,需要修改的地方有配置数据库连接源中的值。另外就是这个映射文件,这个可以写多个,指向的就是编译后的类文件的名称,下面说明这个类文件名称的由来
1)在pom文件的build中加入如下内容,来保证编译时,自己写的xml、properties文件会被一起加载过去
<resources>
<resource>
<directory>src/main/java</directory>
<!--true:对资源进行过滤,即跳过、滤除-->
<!--默认false-->
<filtering>false</filtering>
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
</includes>
</resource>
<resource>
<directory>src/main/researse</directory>
<filtering>false</filtering>
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
</includes>
</resource>
</resources>
2)在compile后生成的target包中找到StudentDao.xml然后copy path->Path from content root得到target/path/dao/StudentDao.xml只取到dao/StudentDao.xml写入 主配置文件的mappers中
5.创建sqlsession执行,在Java包下面创建一个执行sqlsession的类,在创建前需要导入依赖
<dependency>
<groupId>org.apache.ibatis</groupId>
<artifactId>ibatis-sqlmap</artifactId>
<version>2.3.4.726</version>
</dependency>
再写入
public class MybatisDemo {
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();
//指定要执行sql语句的标识
String sqlid = "dao.StudentDao"+"."+"selectStudents";//命名空间+.+select id
List<Student> studentList =sqlSession.selectList(sqlid);
studentList.forEach(s-> System.out.println(s));
sqlSession.close();
}
}
这是一个简单的查询例子,下面开始介绍插入操作,并且封装重复的操作
插入操作:
1)在上面的基础上,新增接口方法
public int insertStudents(Student student);
2) 在StudentDao.xml中加入以下内容,其中#{}为占位符,内容填实体类的属性
<insert id="insertStudents">
insert into student values (#{id},#{name},#{email},#{age})
</insert>
3)在测试类中,修改下面配置
String sqlid = “dao.StudentDao.insertStudents”;
Student student = new Student();
student.setId(102);
student.setName(“刘备”);
student.setAge(29);
student.setEmail(“8079244242@qq.com”);
int insert = sqlSession.insert(sqlid, student);
sqlSession.commit();//手动提交事务,自动提交时,只需要在SqlSession sqlSession = factory.openSession();中加入true即SqlSession sqlSession = factory.openSession(true);即可自动提交
以上操作结束后,可以在数据库中查到数据,若需要调试输出到控制台,则需要配置日志问价,在主要配置文件中加入以下信息
<settings>
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>
最后,封装一下测试类中重复的代码:
1)在Java包下新建utils.MyUtils.java
public class MyUtils {
private static SqlSessionFactory factory = null;
static {
String config ="Mybatis.xml";
try {
InputStream in = Resources.getResourceAsStream(config);
factory = new SqlSessionFactoryBuilder().build(in);
} catch (IOException e) {
e.printStackTrace();
}
}
public static SqlSession getSqlSession(){
SqlSession sqlSession = null;
if (factory != null){
//采用非自动提交方式
sqlSession=factory.openSession();
}
return sqlSession;
}
}
2)测试类书写:
public class Mybatis2 {
@Test
public void insert() throws IOException {
SqlSession sqlSession = MyUtils.getSqlSession();
String sqlid = "dao.StudentDao.selectStudents";
List<Student> list = sqlSession.selectList(sqlid);
list.forEach((s) -> System.out.println(s));
sqlSession.close();
}
}
开始改进,传统的dao实现方式,无非将测试类中的操作放到接口的实现类中,通过测试类调用实现类的方法,这里简要介绍部分如下图。
public class StudentDaoImpl implements StudentDao {
@Override
public List<Student> selectStudents() {
SqlSession sqlSession = mybatisUtils.getSqlSession();
String sqlid = "dudu.dao.StudentDao.selectStudents";
List<Student> res = sqlSession.selectList(sqlid);
sqlSession.close();
return res;
}
@Override
public int insertStudent(Student student) {
SqlSession sqlSession = mybatisUtils.getSqlSession();
String sqlid ="dudu.dao.StudentDao.insertStudent";
int sum = sqlSession.insert(sqlid, student);
sqlSession.commit();
sqlSession.close();
return sum;
}
}
传统方式测试
// @Test
// public void getSelect(){
// StudentDao studentDao = new StudentDaoImpl();
// List<Student> students = studentDao.selectStudents();
// students.forEach(a-> System.out.println(a));
// }
// @Test
// public void putData(){
// StudentDao studentDao = new StudentDaoImpl();
// Student student = new Student();
// student.setId(1007);
// student.setName("汤姆布鲁斯");
// student.setEmail("gh219847@199.com");
// student.setAge(46);
// int num = studentDao.insertStudent(student);
// System.out.println("改变了"+num+"行数据!");
// }
相比于传统方式,mybatis的动态代理可以帮你去完成这个实现类,mybatis会根据dao的接口,创建一个接口实现类,在实现类中调用Sqlsession方法执行sql语句,访问数据库。
在删除实现方法后,用sqlSession.getMapper(StudentDao.class);获取接口
@Test
public void getResult(){
SqlSession sqlSession = mybatisUtils.getSqlSession();
StudentDao studentDao = sqlSession.getMapper(StudentDao.class);
List<Student> students = studentDao.selectStudents();
for (Student s: students) {
System.out.println(s);
}
}
@Test
public void testInsert(){
SqlSession sqlSession = mybatisUtils.getSqlSession();
StudentDao mapper = sqlSession.getMapper(StudentDao.class);
Student student = new Student();
student.setId(1078);
student.setName("二姑");
student.setAge(65);
student.setEmail("46244242@qq.com");
int i = mapper.insertStudent(student);
sqlSession.commit();
sqlSession.close();
System.out.println("增加了"+i+"条数据!");
}
传递参数,分为传递一个参数,直接用占位符#{};传递多个参数,使用注解@Param(“别名”)例如
public List selIDorName(@Param(“myname”) String name, @Param(“myage”) Integer age);
再在xml中使用占位符,将别名传递进去。
传递一个对象:类似于上文中的插入数据,就是新建一个对象,里面存放要查询的属性,生成相应的getter与setter方法,在xml文件中直接使用其属性名,测试时创建这个对象实例即可。
实际开发中要掌握的这三种。
了解的传参数还有1)按位置即用占位符#{arg0}代表第一个位置,同理#{arg1}代表第2个位置.2)按照map传值,接口方法中传入(Map(String,Object) map),然后将测试类中创建的map对象中的key值传入占位符#{key}且只返回一行记录
KaTeX parse error: Expected 'EOF', got '#' at position 2: 与#̲用法区别 1)#使用?在SQL…{}在sql语句中是字符串的拼接,直接将你传递的实参连接到sql语句上,其使用Statement对象执行sql,效率较低
4)有sql注入风险,不安全
5)$常常用在替换列名、表名等
resultType的作用:将sql执行后的列的数据转为Java对象student
不能说resultType中一定存放实体类,其类型是任意的,其实可以将其看作是接口方法的返回值类型。
所以如果想转换对象可以灵活使用resultType,如果是基本数据类型+String可以写全限定名称或者别名
配置其中的别名。
<typeAliases>
<!-- 第一种方式,将返回值全限定名直接更改-->
<typeAlias type="dudu.domain.Student" alias="stu"></typeAlias>
<!-- 第二种方式,包名中的类名就是别名-->
<package name="dudu.domain"/>
</typeAliases>
resultMap可以指定列名与Java属性的对应关系,当想列名与属性名不一样时可以使用其进行变化。
使用案例如下:先定义,其中id是主键列,column对应的查询列,property对应对象的属性
<resultMap id="studentMap" type="dudu.domain.Student">
<id column="id" property="id"></id>
<result column="name" property="email"></result>
<result column="email" property="name"></result>
<result column="age" property="age"></result>
<!--resultType="dudu.domain.Student"-->
</resultMap>
<select id="selectStudents" resultMap="studentMap">
select id,name,email,age from student order by id
</select>
可以理解成column->property赋值
resultMap与resultType不可同用
上面例子如果用resultType,可以将列名的别名写成对象的属性名称
模糊查询中
1)select …where name like “%” #{name} “%” 注意有空格百分号与占位符之间
2)或者直接写占位符#{name},再在测试方法中定义一个字符串String s = “%刘%”
动态sql
java对象作为参数
满足条件后的赋值
where:可以包含多个if,会智能的拼接字符串
foreach:用在in条件语句,对集合遍历得到限定结果
级联查询
案例1、https://www.cnblogs.com/xdp-gacl/p/4264440.html
案例2、https://www.bilibili.com/video/BV1KK4y1a7bv?p=7&spm_id_from=pageDriver
The server time zone value ‘�й���ʱ��’ is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the ‘serverTimezone’ configuration property) to use a more specifc time zone value if you want to utilize time zone support.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
解决办法:
MySql时区报错
在[mysqld]下方添加:default-time-zone=’+08:00’