使用maven项目,前面有详解如何配置maven
mybatis,主要就是在mvc中的dao层,用于对数据库进行操作,接下来可以分成一条线,相当于工厂模式一样掌握这个mybatis
1,目录
2,编写pojo中的实体类
package com
.pojo
;
import lombok
.AllArgsConstructor
;
import lombok
.Data
;
import lombok
.NoArgsConstructor
;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Student {
private String name
;
private String password
;
private Integer age
;
private String address
;
}
3,编写数据库,根据实体类的参数以及数据类型编写数据库,这边使用外面的图形化界面编写的,使用的是mysql数据库,在哪编写都一样,能连上就行
4,编写操作实体类的接口,即dao中的StudentI类
package com
.dao
;
import com
.pojo
.Student
;
import java
.util
.List
;
public interface StudentI {
public int insert(Student stu
);
public int delete(String name
);
public int update(Student student
);
public List
<Student> select();
}
5,编写实现接口的xml文件,即dao中的StudentI.xml文件
注意:一个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="com.dao.StudentI">
<insert id="insert" parameterType="Student">
insert into student values (#{name},#{password},#{age},#{address})
</insert>
<delete id="delete">
delete from student where name = #{name}
</delete>
<update id="update" parameterType="Student">
update student name = #{name},password=#{password},age=#{age} where address=#{address}
</update>
<select id="select" resultType="Student">
select * from student
</select>
</mapper>
6,mybatis的可信配置文件,相当于一个工厂,需要将前面的实现接口的xml文件全部装在这个工厂中,并在这个工厂中还需要配置sql连接数据库的参数,以的形式将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>
<!--日志
-->
<settings>
<setting name
="logImpl" value
="STDOUT_LOGGING"/>
</settings
>
<!--别名
-->
<typeAliases>
<typeAlias type
="com.pojo.Student" alias
="Student"></typeAlias
>
</typeAliases
>
<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?serverTimezone=GMT+8"/>
<property name
="username" value
="root"/>
<property name
="password" value
="zhs03171812"/>
</dataSource
>
</environment
>
</environments
>
<mappers>
<mapper resource
="com/dao/StudentI.xml"/>
</mappers
>
</configuration
>
7,在将所有的文件装入到工厂中,我们需要去去,相当于spring一样,由容器将bean创建好,有外部调用;这里也是,我们需要将工厂中的sqlSessionFactory工厂中的sqlSession的资源加载出。
因此可以创建一个工具类,将资源sqlsession资源加载出
package com
.tool
;
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
.InputStream
;
public class UtilGetSqlSession {
private static SqlSessionFactory sqlsessionFactory
;
static {
try{
String resources
= "mybatis-config.xml";
InputStream resourceAsStream
= Resources
.getResourceAsStream(resources
);
sqlsessionFactory
= new SqlSessionFactoryBuilder().build(resourceAsStream
);
}catch(Exception exeption
){
exeption
.printStackTrace();
}
}
public static SqlSession
getSqlSession(){
SqlSession sqlSession
= sqlsessionFactory
.openSession();
return sqlSession
;
}
}
8,在test测试的根目录下创建测试类
对每一个增删改查都进行了测试,使用了多个juint
import com
.dao
.StudentI
;
import com
.pojo
.Student
;
import com
.tool
.UtilGetSqlSession
;
import org
.apache
.ibatis
.session
.SqlSession
;
import java
.util
.List
;
public class Test {
@org.junit
.Test
public void insert(){
SqlSession sqlsession
= UtilGetSqlSession
.getSqlSession();
StudentI mapper
= sqlsession
.getMapper(StudentI
.class);
mapper
.insert(new Student("qwq","123456",20,"广东"));
System
.out
.println("数据插入成功");
sqlsession
.commit();
sqlsession
.close();
}
@org.junit
.Test
public void delete(){
SqlSession sqlsession
= UtilGetSqlSession
.getSqlSession();
StudentI mapper
= sqlsession
.getMapper(StudentI
.class);
mapper
.delete("qaq");
System
.out
.println("数据删除成功");
sqlsession
.commit();
sqlsession
.close();
}
@org.junit
.Test
public void update(){
SqlSession sqlsession
= UtilGetSqlSession
.getSqlSession();
StudentI mapper
= sqlsession
.getMapper(StudentI
.class);
mapper
.update(new Student("qqq","123123",21,"江西"));
sqlsession
.commit();
sqlsession
.close();
}
@org.junit
.Test
public void select(){
SqlSession sqlsession
= UtilGetSqlSession
.getSqlSession();
StudentI mapper
= sqlsession
.getMapper(StudentI
.class);
List
<Student> select
= mapper
.select();
for (Student stu
: select
) {
System
.out
.println(stu
);
}
sqlsession
.commit();
sqlsession
.close();
}
}
9,验证
在每一个测试完之后,可以去表中刷新查看 在使用mybatis时,不太推荐使用注解!