😊😊😊欢迎来到本博客😊😊😊
📫作者简介:阿斯卡码,专注于研究Java框架/Vue,就读于河南中医药大学,刚刚入门项目开发📫
🏆 CSDN编程比赛奖章获得者/Java领域创作者🏆
🔥计划学习:深入学习Spring全家桶,Vue, mybatis,Mysql等领域。(目前涉及不深入)🔥
👍如果此文还不错的话,还请👍关注、点赞、收藏三连支持👍一下博主~
项目代码:
链接:https://pan.baidu.com/s/1POo7Gscj3JgnUji9sXmWbw
提取码:6666
–来自百度网盘超级会员V3的分享
mapper下的xml文件都会被package识别。
pojo下的所有类都会被package识别。
所以递归也会识别。
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/Stu?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8
#jdbc.url=jdbc:mysql://localhost:3306/?????????????serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8
jdbc.username=root
jdbc.password=XXXXXXXX
DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<properties resource="Application.properties" />
<settings>
<setting name="logImpl" value="STDOUT_LOGGING"/>
settings>
<typeAliases>
<package name="com.mybatis.ch01.pojo"/>
typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
dataSource>
environment>
environments>
<mappers>
<package name="com.mybatis.ch01.mapper"/>
mappers>
configuration>
package com.mybatis.ch01.utils;
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;
/**
* 工具类:创建SqlSession对象
*/
public class MyBatisUtil {
private static SqlSessionFactory factory = null;
static {
String config= "mybatis-config.xml";
try {
InputStream inputStream =Resources.getResourceAsStream(config);
factory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
//创建方法,获取SqlSession对象
public static SqlSession getSqlSession(){
SqlSession session = null;
if( factory != null ){
session =factory.openSession(true);// openSession(true);
}
return session;
}
}
build标签:
<resources>
<resource>
<directory>src/main/javadirectory>
<includes>
<include>**/*.propertiesinclude>
<include>**/*.xmlinclude>
includes>
<filtering>falsefiltering>
resource>
resources>
dependencies标签
//Test测试使用
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.11version>
<scope>testscope>
dependency>
//JDBC使用
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>8.0.30version>
dependency>
//mybatis依赖
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatisartifactId>
<version>3.4.6version>
dependency>
//@Data注解需要
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<version>1.18.12version>
<scope>providedscope>
dependency>
Student.java
package com.mybatis.ch01.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class Student {
private Integer id;
private String name;
private String email;
private Integer age;
}
Teacher.java
package com.mybatis.ch01.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class Teacher {
private Integer id;
private String name;
private String tno;
private String classname;
private Integer money;
}
ST.java:导出类
package com.mybatis.ch01.pojo.utils;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class ST {
private Integer studentid;
private String studentname;
private Integer teacherid;
private String teachername;
}
StudentMapper.java
package com.mybatis.ch01.mapper.student;
import com.mybatis.ch01.pojo.Student;
import org.apache.ibatis.annotations.Param;
import java.util.List;
import java.util.Map;
public interface StudentMapper {
//传入一个简单类型的参数
Student selectByIdOne(Integer id);
//传入多个简单类型的参数
List<Student> selectByNameEmailTwo (@Param("myName") String name,
@Param("myEmail") String email
);
//传入一个对象
List<Student> selectByNameEmailTwo2 (Student student);
//使用位置,获取参数
List<Student> selectByNameEmailTwo3 (String name,String email);
//参数是一个Map
List<Student> selectByNameEmailTwo4 (Map<String,Object> data);
}
StudentMapper.xml
DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.mybatis.ch01.mapper.student.StudentMapper">
<select id="selectByIdOne" resultType="Student">
select * from student where id = #{myId}
select>
<select id="selectByNameEmailTwo" resultType="Student">
select * from student where email = #{myEmail} and name = #{myName}
select>
<select id="selectByNameEmailTwo2" resultType="Student">
select * from student where email = #{email} and name = #{name}
select>
<select id="selectByNameEmailTwo3" resultType="Student">
select * from student where name = #{arg0} and email = #{arg1}
select>
<select id="selectByNameEmailTwo4" resultType="Student">
select * from student where name = #{name} and email = #{email}
select>
mapper>
TeacherMapper.java
package com.mybatis.ch01.mapper.teacher;
import com.mybatis.ch01.pojo.Teacher;
import org.apache.ibatis.annotations.Param;
import java.util.List;
import java.util.Map;
public interface TeacherMapper {
List<Teacher> selectByTows(Map<String,Object> data);
}
TeacherMapper.xml
DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.mybatis.ch01.mapper.teacher.TeacherMapper">
<select id="selectByTows" resultType="Teacher">
select * from teacher where classname = #{classname} and money = #{money}
select>
mapper>
重点:
STMapper.java
package com.mybatis.ch01.mapper.utils;
import com.mybatis.ch01.pojo.utils.ST;
import java.util.List;
public interface STMapper {
List<ST> getList();
}
DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.mybatis.ch01.mapper.utils.STMapper">
<select id="getList" resultType="ST">
select s.id studentid,s.name studentname,t.id teacherid,t.name teachername from student s,teacher t where s.id = t.id
select>
mapper>
package com.mybatis.ch01;
import static org.junit.Assert.assertTrue;
import com.mybatis.ch01.mapper.student.StudentMapper;
import com.mybatis.ch01.mapper.teacher.TeacherMapper;
import com.mybatis.ch01.mapper.utils.STMapper;
import com.mybatis.ch01.pojo.Student;
import com.mybatis.ch01.pojo.Teacher;
import com.mybatis.ch01.pojo.utils.ST;
import com.mybatis.ch01.utils.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Unit test for simple App.
*/
public class AppTest
{
/**
* 传入一个简单类型的参数
*/
@Test
public void selectByIdOne()
{
//1.获取SqlSession
SqlSession session = MyBatisUtil.getSqlSession();
//2.获取dao的代理
StudentMapper dao = session.getMapper(StudentMapper.class);
Student stu = dao.selectByIdOne(1007);
System.out.println(stu);
//3.关闭SqlSession对象
session.close();
}
/**
* 传入多个简单类型的参数
*/
@Test
public void selectByNameEmailTwo()
{
SqlSession session = MyBatisUtil.getSqlSession();
StudentMapper dao = session.getMapper(StudentMapper.class);
List<Student> stus = dao.selectByNameEmailTwo("yxc1007","yxc1@qq.com");
for (Student student:stus){
System.out.println("stu==" + student);
}
session.close();
}
/**
* 传入一个对象
*/
@Test
public void selectByNameEmailTwo2()
{
SqlSession session = MyBatisUtil.getSqlSession();
StudentMapper dao = session.getMapper(StudentMapper.class);
Student student = new Student();
student.setName("yxc1007");
student.setEmail("yxc1@qq.com");
List<Student> stus = dao.selectByNameEmailTwo2(student);
for (Student stu:stus){
System.out.println("stu==" + stu);
}
session.close();
}
/**
* 使用位置,获取参数
*/
@Test
public void selectByNameEmailTwo3()
{
SqlSession session = MyBatisUtil.getSqlSession();
StudentMapper dao = session.getMapper(StudentMapper.class);
List<Student> stus = dao.selectByNameEmailTwo3("yxc1007","yxc1@qq.com");
for (Student stu:stus){
System.out.println("stu==" + stu);
}
session.close();
}
/*
使用Map作为参数
*/
@Test
public void selectByNameEmailTwo4()
{
SqlSession session = MyBatisUtil.getSqlSession();
StudentMapper dao = session.getMapper(StudentMapper.class);
Map<String,Object> data = new HashMap<>();
data.put("name","yxc1007");
data.put("email","yxc1@qq.com");
List<Student> stus = dao.selectByNameEmailTwo4(data);
for (Student stu:stus){
System.out.println("stu==" + stu);
}
session.close();
}
/*
使用Map作为参数(练习2)
*/
@Test
public void selectByTwos()
{
SqlSession session = MyBatisUtil.getSqlSession();
TeacherMapper dao = session.getMapper(TeacherMapper.class);
Map<String,Object> data = new HashMap<>();
data.put("classname","计科");
data.put("money",3000);
List<Teacher> teachers = dao.selectByTows(data);
for (Teacher tea:teachers){
System.out.println("tea==" + tea);
}
session.close();
}
/**
* 返回多表的数据
*/
@Test
public void getList()
{
SqlSession session = MyBatisUtil.getSqlSession();
STMapper dao = session.getMapper(STMapper.class);
List<ST> sts = dao.getList();
for (ST s:sts){
System.out.println("st==" + s);
}
session.close();
}
}