Mbatis分页等

一、Mbatis分页

通过GitHub插件 进行mybatis分页数据获取 (无前台实现)

原理 : 通过geiHub的

1主配置文件加载GitHub插件

<plugins>
<plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
</plugins>
或者
<plugin interceptor="com.github.pagehelper.PageInterceptor">
<!-- 设置数据库类型 Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六种数据库-->
<!---<property name="helperDialect" value="mysql"/>--->
</plugin>

在这里插入图片描述
2.pom.xml加载 插件

<!-- mybatis 拦截器-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.4</version>
</dependency>

3.映射文件编写sql语句

<select id="selectpage" resultType="java.util.Map">
select * from user
</select>

4.接口

public interface UserMapper {
// 分页
List<Map> selectpage();
}

5.测试类

public class MainTest01 {
public static void main(String[] args) {
try {
InputStream in= Resources.getResourceAsStream("config/mybatis.xml");
SqlSessionFactoryBuilder builder=new SqlSessionFactoryBuilder();
SqlSessionFactory build = builder.build(in);
SqlSession session = build.openSession();
UserMapper mapper = session.getMapper(UserMapper.class);
//gitHub 插件
PageHelper.startPage(1,3); //从第一个 拿出3个
List<Map> selectpage = mapper.selectpage();
//System.out.println(selectpage);
PageInfo pageInfo=new PageInfo(selectpage);
System.out.println(pageInfo.getSize());//当前页的内容条数 3
System.out.println(pageInfo.getPageSize());//一共页数 3
System.out.println(pageInfo.getStartRow());//获取起始行 1
System.out.println(pageInfo.getPageNum());//获取页码 1
System.out.println(pageInfo.getPages());//获取页面 3
System.out.println(pageInfo.getNavigatePages());//获取导航页面 8
System.out.println(pageInfo.getList());//当前页的内容
} catch (IOException e) {
e.printStackTrace();
}
}
}

二、mybatis执行语句的原理

启用的类
SqlSessionFacotryBuilder:创建sqlSessionFacotry对象的。(依赖主配置⽂件)
SqlSessionFactory:重量级。⼀般⼀个数据源⼀个。
SqlSession:sqlSessionFacoty.open打开的。对应的是数据库的⼀次链接。⽬的是进⾏增 删改查。

主要成员
Configuration MyBatis所有的配置信息都保存在Configuration对象之中 ,配置文件中的大部分都会存储到该类
SqlSession MyBatis工作的主要顶层API ,表示和数据库交互时的会话 完成必要的数据库增删改查
Executor 执行器,mybatis的调度核心 负责SQL语句的生成和查询缓存的维护
StatementHander 封装了JDBC Statement操作,负责对JDBC statement 的操作,如设置参数等
ParameterHander 负责对⽤户传递的参数转换成JDBC Statement 所对应的数据类型
ResultSetHandler 负责将JDBC返回的ResultSet结果集对象转换成List类型的集合(结果集处理)
TypeHandler 负责java数据类型和jdbc数据类型(也可以说是数据表列类型)之间的映射和转换(类型处理器)
MappedStatement MappedStatement维护⼀条<select|update|delete|insert>节点的封装(映射语句)
SqlSource 负责根据⽤户传递的parameterObject,动态地⽣成SQL语句,将信息封装到BoundSql对象中,并返回(数据源)
BoundSql 表示动态⽣成的SQL语句以及相应的参数信息 ()

在这里插入图片描述
在这里插入图片描述

新增返回主键

注意

JDBC 要求,如果⼀个列允许使⽤ null 值,并且会使⽤值为 null 的参数,就必须要指定 JDBC 类型(jdbcType)

在这里插入图片描述
主键允许自增
映射文件

<!--insert返回还是影响条数 住建:放到传递过来的实体属性中或者Map的一个key中 -->
<!-- 新增返回主键 数据库主键允许自增 方式 keyProperty=关键属性 通过打开 useGeneratedKeys=使用生成的密钥 自己生成了id -->
<insert id="insertMapkey" keyProperty="id" useGeneratedKeys="true" parameterType="com.entity.UserEntity">
insert into user(id,username,pwd) values (null,#{username},#{pwd})
</insert>
主键不能自增的情况下
<!--insert返回还是影响条数 住建:放到传递过来的实体属性中或者Map的一个key中 varchar -->
<insert id="insertMapkeyk" parameterType="com.entity.UserEntity" >
<selectKey resultType="java.lang.Integer" order="BEFORE" keyProperty="id">
select MAX(id)+1 from user
</selectKey>
insert into user(id,username,pwd) values (#{id},#{username,jdbcType=VARCHAR},#{pwd})
</insert>

测试类

public static void main(String[] args) {
try {
InputStream in= Resources.getResourceAsStream("config/mybatis.xml");
SqlSessionFactoryBuilder builder=new SqlSessionFactoryBuilder();
SqlSessionFactory build = builder.build(in);
SqlSession session = build.openSession();
UserMapper mapper = session.getMapper(UserMapper.class);
UserEntity entity=new UserEntity();
entity.setPwd("199556");
entity.setUsername("张三试试");
Integer integer = mapper.insertMapkey(entity);
System.out.println(entity);// 结果 UserEntity{id=28, username='张三试试', pwd='199556'}
} catch (IOException e) {
e.printStackTrace();
}
}

# 三、SpringIOC
## spring简介
Spring 是⼀个开源框架,是为了解决企业应⽤程序开发复杂性⽽创建 的。框架的主要优势之⼀就是其分层架构,分层架构允许选择使⽤哪⼀个组
件,同时为 J2EE 应⽤程序开发提供集成的框架。

## 什么是ioc
控制反转 inverse of Control 创建对象的权限 ,java 程序中需要用到的对象不在由程序员创建 而是交给 IOC容器来创建。
扩展性增加
耦合度降低

## IOC接口

BeanFactory:

~~是spring的内部接口 不提供给开发人员使用,加载配置文件时不会创建对象,在获取对象才创建对象

BeanFactory由org.springframework.beans.factory.BeanFactory接⼝定义 BeanFactory是⼯⼚模式(Factory pattern)的实现,是IOC容器的核⼼接⼝,它的职责包括: 实例化、定位、配置应⽤程序中的对象及建⽴这些对象间的依赖。

BeanFactory接⼝包含以下基本⽅法——ApplicationContext肯定都有,因为他是⼦接⼝, 加强版
boolean containsBean(String beanName) 判断⼯⼚中是否包含给定名称的bean定 义,若有则返回true

Object getBean(String) 返回给定名称注册的bean实例。根据bean的配置情况,如果 是singleton模式将返回⼀个共享实例,否则将返回⼀个新建的实例,如果没有找到指定 bean,该⽅法可能会抛出异常

Object getBean(String, Class) 返回以给定名称注册的bean实例,并转换为给定class 类型Class getType(String name) 返回给定名称的

bean的Class,如果没有找到指定的bean 实例,则排除NoSuchBeanDefinitionException异常

boolean isSingleton(String) 判断给定名称的bean定义是否为单例模式

String[] getAliases(String name) 返回给定bean名称的所有别名~~

BeanFactory Context = new ClassPathXmlApplicationContext("com/mapper/applicationContest.xml");
ApplicationContext:
ApplicationContext Context = new ClassPathXmlApplicationContext("com/mapper/applicationContest.xml");

ApplicationContext是基于BeanFactory之上的,提供了应⽤程序框架服务,扩展的新功能如 下:提供
BeanFactory接口的子接口,提供更多更强大的功能 ,一般由开发人员进行使用加载配置文件的时候就会吧配置文件对象进行创建
两个实现类

在这里插入图片描述

小案例操作Bean管理

1、创建对象 注入属性
使用bean标签 向里面添加对应属性 可以实现对象创建
id属性是起别名 :唯一标识
class属性 : 类全路劲(包类)
创建对象时默认创建无参构造完成对象创建

<bean id="stuMapper" class="com.mapper.StudetnMapper"></bean>

2、两种方式

(1)xml配置文件

<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
<!-- 将studetnMapper类当做㝉类来管理 class 包名 id/name :别名 id是唯一标识 name可以重名 name可以用符合 -->
<!-- 默认对象在创建的时候回将对象new 出来 默认new 一次 new 的时候调用 init方法
destory : 对象销毁的时候触发
lay-inti : true 懒加载什么时候get什么时候创建对象
scope:
singleton: 单例 :每个单例只会new一个
prototype : 非单例 每get一次就会new一次
-->
<!--autowire : 自动注入
byType:根据类型自动匹配其他 bean 然后将其注入
但是如果有同类型不同id时会报错
byName:根据属性名去找对应的name或者id为指定属性的bean进行注入
no:关闭
constructor:构造注入
-->
<bean id="stuMapper" class="com.Mapper.StudetnMapper"></bean>
<bean id="student" class="com.Mapper.Student">
<property name="mapper" ref="stuMapper"></property>
</bean>
</beans>

在这里插入图片描述

public class BennTest {
public static void main(String[] args) {
ApplicationContext context =new ClassPathXmlApplicationContext("bean.xml");
//FileSystemXmlApplicationContext 相对于一个项目的一个绝对路劲
//ClassPathXmlApplicationContext 相对于classpath的根路劲
//AnnotationConfigApplicationContext classpath路径加载xml⽂件的
//WebApplicationContext:web⼯程整合后
Student student = (Student) context.getBean("student");
student.select();
}
}

输出 kqc

(2)基于注解

@Component("StudetnMapper")//设置调用的名称
public class StudetnMapper {
private String name;
public void selecttt(){
System.out.println("这个是StudentMapper2");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
@Component("StudentService")
public class StudentService {
@Value("zhangyf")
private String name;
@Autowired
private StudetnMapper mapper;
public List select(){
System.out.println(name);
mapper.selecttt();
return null;
}
public void setMapper(StudetnMapper mapper) {
this.mapper = mapper;
}
@Override
public String toString() {
return "StudentService{" +
"name='" + name + '\'' +
", mapper=" + mapper +
'}';
}
}
public class MainTest {
public static void main(String[] args) {
ApplicationContext context=new ClassPathXmlApplicationContext("config/bean2.xml");
StudentService studentService = (StudentService) context.getBean("StudentService");
studentService.select();
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="bean2.service bean2.Mapper"/><!-- 最重要的解析注解-->
</beans>

(3)java配置(没什么用)

public class StudetnMapper {
private String name;
public void selecttt(){
System.out.println("这个是StudentMapper2");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
/**
* @author zhangyifan
* @version 8.0
* @description:
* @date 2021/10/26 14:48
*/
public class StudentService {
private String name;
@Autowired
private StudetnMapper mapper;
public List select(){
System.out.println(name);
mapper.selecttt();
return null;
}
public void setMapper(StudetnMapper mapper) {
this.mapper = mapper;
}
@Override
public String toString() {
return "StudentService{" +
"name='" + name + '\'' +
", mapper=" + mapper +
'}';
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
/**
* @author zhangyifan
* @version 8.0
* @description:
* @date 2021/10/26 15:29
*/
/*@Configuration
@ComponentScan("bean3")*/
public class ApplicationContext {
@Bean
public StudetnMapper getStudenMapper(){
StudetnMapper mapper=new StudetnMapper();
return mapper;
}
@Bean
public StudentService setStudentService(){
StudentService service=new StudentService();
service.setMapper(this.getStudenMapper());
return service;
}
/**
* @author zhangyifan
* @version 8.0
* @description: 测试类
* @date 2021/10/26 15:29
*/
public class test {
public static void main(String[] args) {
AnnotationConfigApplicationContext context=new AnnotationConfigApplicationContext(ApplicationContext.class);
StudentService setStudentService = (StudentService) context.getBean("setStudentService");
setStudentService.select();
}
}

(4)java配置开启注解扫描

在这里插入图片描述

posted @   啧啧啧|(* ̄ ̄*)  阅读(5)  评论(0编辑  收藏  举报  
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
点击右上角即可分享
微信分享提示