PS:不会用maven创建mybatis项目的,点击查看基础教程。
PS1:maven下载安装与配置:https://blog.csdn.net/m0_47306534/article/details/115261033
PS2:maven创建各种项目教程:
https://blog.csdn.net/m0_47306534/article/details/115320874
PS:不同的项目结构代表不同的数据路劲,如果和博主不一样的,记得修改,否则会报错,或者找不到资源文件。
<dependencies>
<!-- 1.导入mybatis包-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.2</version>
</dependency>
<!-- 2。导入数据库驱动包-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<!-- 注意:如果出现Unkonw 。。。launge的,是本机安装的mysql版本太低
此时换一个低版本的驱动包就好了,例如换成5.1.28或者5.1.24
5.1.24版本代码:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.24</version>
</dependency>
5.1.28版本代码:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.28</version>
</dependency>
-->
<!-- 3.导入测试包-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
可以添加资源适配器:
<!-- SECOND:防止静态资源没有注入到target中-->
<!-- 可以自动配置路径-->
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml
**/ *.properties</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.xml
**/ *.properties</include>
</includes>
</resource>
</resources>
</build>
package com.zyq.pojo;
//实例化student
public class Student {
private int ID;
private String SName;
private String Email;
private String Age;
public Student() {
}
public Student(int ID, String SName, String email, String age) {
this.ID = ID;
this.SName = SName;
Email = email;
Age = age;
}
public int getID() {
return ID;
}
public void setID(int ID) {
this.ID = ID;
}
public String getSName() {
return SName;
}
public void setSName(String SName) {
this.SName = SName;
}
public String getEmail() {
return Email;
}
public void setEmail(String email) {
Email = email;
}
public String getAge() {
return Age;
}
public void setAge(String age) {
Age = age;
}
@Override
public String toString() {
return "Student{" +
"ID=" + ID +
", SName='" + SName + '\'' +
", Email='" + Email + '\'' +
", Age='" + Age + '\'' +
'}';
}
}
package com.zyq.dao;
import com.zyq.pojo.Student;
import org.apache.ibatis.annotations.Param;
import java.util.List;
public interface StudentMapper {
//1.查询所有学生的接口
List<Student> getStuList();
//2.根据id来查询学生
//@Param用来传参数
Student getStuByID(@Param("ID") int ID);
//3.根据学生名来查询
Student getStuByName(@Param("SName") String Name);
//4.删除学生
//@Param用来传参数
int deleteStu(@Param("ID") int ID);
//5.更新学生信息
int updateStu(Student student);
//6.添加学生信息
int addStu(Student student);
//7.根据用户名模糊查询
Student getStuByMhName(String str);
}
PS:编写数据库dbconfig.properties文件
jdbc.username=root
jdbc.password=root
#jdbc.password=root表示密码是root
# 如果你的数据库密码不一样,更改就行,注意不要有空格
jdbc.jdbcUrl=jdbc:mysql://localhost:3306/mybatis
#3306后面的mybatis是数据库的名,如果数据库不一样
#需要更改数据库名
#这里使用的是mybatis数据库下的student表
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.maxPoolSize=20
jdbc.minPoolSize=5
PS:mybatis-config.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核心配置文件-->
<configuration>
<!-- 1.引入数据库资源文件-->
<properties resource="dbconfig.properties"></properties>
<!-- 2.配置数据库核心文件-->
<environments default="development">
<environment id="development">
<!--配置JDBC链接池-->
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<!-- 使用$符号获取数据库资源文件的内容-->
<property name="url" value="${jdbc.jdbcUrl}"></property>
<property name="driver" value="${jdbc.driverClass}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
<!-- 设置最大最小链接数-->
<!-- <property name="maxPoolSize" value="20"></property>-->
<!-- <property name="minPoolSize" value="5"></property>-->
<!-- 关闭链接不自动commit-->
<!-- <property name="autoCommitOnClose" value="false"></property>-->
<!-- 获取链接超时时间-->
<!-- <property name="checkoutTimeout" value="10000"></property>-->
<!-- 获取链接失败重试次数-->
<!-- <property name="acquireRetryAttempts" value="2"></property>-->
</dataSource>
</environment>
</environments>
<!-- 3.注入sql映射文件-->
<!-- 将写好的sql映射文件注册到全局配置文件mybatis-config.xml中 -->
<mappers>
<mapper resource="com/zyq/dao/StudentMapper.xml"/>
</mappers>
</configuration>
PS:此文件与StudentDao都放在同一文件下,否则可能会报错
<?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">
<!-- namespace=绑定一个对应的Dao/Mapper接口 -->
<mapper namespace="com.zyq.dao.StudentMapper">
<!-- id=要实现的接口名字(必须和接口一模一样,不然会报错)
resultType=返回的类型(实体类)
parameterType=接口的定义的类型(传参的时候常用)-->
<!-- 1.查询语句 -->
<!-- 1.1:查询所有用户 -->
<!-- PS:resultMap="MyFull":表示使用自定义的封装规则-->
<!-- 注意:因为我的数据库是mybatis
所以大家如果数据库和我的不一样,一定要修改
不然连不上,查询不到数据
例如:数据库是ssm,则改为:select * from ssm.student;
-->
<select id="getStuList" resultType="com.zyq.pojo.Student" resultMap="MyFull">
<!-- sql语句 -->
select * from mybatis.student;
</select>
<!-- 1.2:根据id查询用户 -->
<select id="getStuByID" resultType="com.zyq.pojo.Student" parameterType="int" resultMap="MyFull">
select *
from mybatis.student
where ID = #{ID};
</select>
<!-- 1.3: 根据用户名查询用户-->
<select id="getStuByName" resultType="com.zyq.pojo.Student">
select *
from mybatis.student
where SName = #{SName};
</select>
<!-- 2.删除用户-->
<delete id="deleteStu" parameterType="int">
delete
from mybatis.student
where ID = #{ID};
</delete>
<!-- 3.更新用户-->
<update id="updateStu" parameterType="com.zyq.pojo.Student">
update mybatis.student
set SName=#{SName},
Email=#{Email},
Age=#{Age}
where ID = #{ID};
</update>
<!-- 4.添加用户-->
<insert id="addStu" parameterType="com.zyq.pojo.Student">
insert into mybatis.student
(ID, SName, Email, Age)
values (#{ID}, #{SName}, #{Email}, #{Age});
</insert>
<!-- 5.模糊查询用户-->
<select id="getStuByMhName" parameterType="com.zyq.pojo.Student" resultMap="MyFull">
select *
from mybatis.student
where SName like "%"#{str}"%";
</select>
<!-- 6.自定义封装规则-->
<!-- 注意:只有使用查询或者级联查询时才会用到,增删改无需使用-->
<!-- column=“”对应的是数据库的字段名,property表示的是实体类的名-->
<resultMap id="MyFull" type="com.zyq.pojo.Student">
<!-- <id>标签里面的属性表示里面的数据库字段是主键-->
<id property="ID" column="ID"></id>
<!-- 普通列:-->
<result property="SName" column="SName"></result>
<result property="Email" column="Email"></result>
<result property="Age" column="Age"></result>
</resultMap>
</mapper>
package com.zyq;
import com.zyq.dao.StudentMapper;
import com.zyq.pojo.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 org.junit.Test;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
public class MyTest {
/*1.查询*/
/*1.1:查询所有用户*/
@Test
public void selectStu() {
System.out.println("查询所有学生的请求正在处理");
//try cath抛出异常,方便找bug
try {
//加载数据文件
String resource = "mybatis-config.xml";
//以下三句普遍适用,加载数据资源创建sql工厂
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession = sqlSessionFactory.openSession(true);
//实例化数据查询接口,获得接口
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
//查询学生,把查询结果放入list集合中
List<Student> student = mapper.getStuList();
//循环输出
for (Student student1 : student) {
System.out.println(student1);
}
//关闭流
sqlSession.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/*1.2:根据id查询用户*/
@Test
public void getStuByID() {
try {
//加载数据文件
String resource = "mybatis-config.xml";
//以下三句普遍适用,加载数据资源创建sql工厂
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession = sqlSessionFactory.openSession(true);
//实例化数据查询接口,获得接口
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
Student student = mapper.getStuByID(3);
System.out.println(student);
sqlSession.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/*1.3根据用户名查询用户*/
@Test
public void getStuByName() {
try {
//加载数据文件
String resource = "mybatis-config.xml";
//以下三句普遍适用,加载数据资源创建sql工厂
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession = sqlSessionFactory.openSession(true);
//实例化数据查询接口,获得接口
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
Student student = mapper.getStuByName("河图");
System.out.println(student);
} catch (IOException e) {
e.printStackTrace();
}
}
//暂未写,剩下的基础大家自己写
/*2.删除用户*/
/*3.添加用户*/
/*4.修改用户*/
}
出现bug的原因很可能是编译的时候,StudentMapper.xml没有注入到target中:
ps:这是成功注入的,如果只有StudentDao说明没有注入成功。
手动注入:
方法一:在pom.xml中添加资源适配,再次编译:
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml
**/ *.properties</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.xml
**/ *.properties</include>
</includes>
</resource>
</resources>
</build>
方法二:在mybatis-config.xml中手动注入:
<mappers>
<mapper resource="com/zyq/dao/StudentMapper.xml"/>
</mappers>
-----------------------------------------------------------------------------------------
到此结束
GJS纯洁