当前位置: 首页 > news >正文

企业培训平台高中同步测控优化设计答案

企业培训平台,高中同步测控优化设计答案,办公室装修效果图片大全,手机网站建设 新闻Mybatis快速入门 一、Mybatis简介1.1Mybatis简化JDBC 二、Mybatis快速入门2.1创建user表,添加数据2.2创建模块,导入坐标2.3编写Mybatis核心配置文件 --> 替换连接信息,解决硬编码问题2.4编写SQL映射文件 --> 统一管理sql语句&#xff0…

Mybatis快速入门

  • 一、Mybatis简介
    • 1.1Mybatis简化JDBC
  • 二、Mybatis快速入门
    • 2.1创建user表,添加数据
    • 2.2创建模块,导入坐标
    • 2.3编写Mybatis核心配置文件 --> 替换连接信息,解决硬编码问题
    • 2.4编写SQL映射文件 --> 统一管理sql语句,解决硬编码问题
    • 2.5编码
      • 2.5.1定义pojo类
      • 2.5.2加载核心配置文件,获取SqlSessionFactory对象
      • 2.5.3获取SqlSession对象,执行SQL语句
  • 总结

一、Mybatis简介

1.1Mybatis简化JDBC

简介:

  • Mybatis是一款优秀的持久层框架,用于简化JDBC开发
  • Mybatis本是Apache的一个开源项目iBatis,2010年这个项目由apache software foundation 迁移到了gppgle code,并且改名为Mybatis。2013年11月迁移到Github
  • 官网:https://mybatis.org/mybatis-3/zh/index.html
  • 持久层:
    • 负责将数据保存到数据库的那一层代码
    • JavaEE三层架构:表现层、业务层、持久层
  • 框架:
    • 框架就是一个半成品软件,是一套可重用的、通用的、软件基础代码模型
    • 在框架的基础之上构建软件编写更加高效、规范、通用、可扩展

JDBC缺点:

  • 1.硬编码
    • 注册驱动,获取连接
    • SQL语句
  • 2.操作繁琐
    • 手动设置参数
    • 手动封装结果集

Mybatis简化JDBC

  • 配置文件
    • Mybatis将一些注册驱动的步骤写入配置文件中,将来直接读取配置文件里的信息就可以了
    • SQL语句也是直接被抽取到配置文件中,在对应的配置文件中直接写SQL语句即可
    • 参数封装通过映射的关系,就可以将结果封装起来,在调用时只需调用该映射中的方法直接使用。

Mybatis免除了几乎所有的JDBC代码以及设置参数和结果集的工作。

二、Mybatis快速入门

需求:查询user表中所有数据

  • 1.创建user表,添加数据
  • 2.创建模块,导入坐标
  • 3.编写Mybatis核心配置文件 --> 替换连接信息,解决硬编码问题
  • 4.编写SQL映射文件 --> 统一管理sql语句,解决硬编码问题
  • 5.编码
    • 1.定义pojo类
    • 2.加载核心配置文件,获取SqlSessionFactory对象
    • 3.获取SqlSession对象,执行SQL语句
    • 4.释放资源

2.1创建user表,添加数据

  • 在对应的mysql数据库中,先创建一个数据库mybatis,再建一个表,表名为tb_user,创建代码如下:
create database mybatis;
use mybatis;drop table if exists tb_user;create table tb_user(id int primary key auto_increment,username varchar(20),password varchar(20),gender char(1),addr varchar(30)
);INSERT INTO tb_user VALUES (1, 'zhangsan', '123', '男', '北京');
INSERT INTO tb_user VALUES (2, '李四', '234', '女', '天津');
INSERT INTO tb_user VALUES (3, '王五', '11', '男', '西安');

在这里插入图片描述

2.2创建模块,导入坐标

  • 1.创建一个Maven的项目
  • 1.1点击File->NEW->Project

在这里插入图片描述


  • 1.2.选择Maven->NEXT

在这里插入图片描述


  • 1.3.在NAME中输入项目名称,点击Finish就创建好了

在这里插入图片描述


  • 2配置pom.xml内的坐标
  • 2.1根据mybatis官网上的步骤,需要将mybatis的依赖导入到pom.xml文件中,即如下代码:
	<!--mybatis 依赖--><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>3.5.13</version></dependency>
  • 2.2要连接数据库,就需要导入mysql驱动:
	<!-- mysql 驱动 --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.32</version></dependency>
  • 2.3单元测试:
	<!--    junit单元测试  --><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.13</version><scope>test</scope></dependency>
  • 2.4为了观察方便,可以添加一些日志的依赖:
	<!-- 添加slf4j日志api --><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-api</artifactId><version>1.7.20</version></dependency><!-- 添加logback-classic依赖 --><dependency><groupId>ch.qos.logback</groupId><artifactId>logback-classic</artifactId><version>1.2.3</version></dependency><!-- 添加logback-core依赖 --><dependency><groupId>ch.qos.logback</groupId><artifactId>logback-core</artifactId><version>1.2.3</version></dependency>
  • 再导入日志依赖的同时,还需要引入一个配置文件logback.xml将它放在resources目录下,内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<configuration><!--CONSOLE :表示当前的日志信息是可以输出到控制台的。--><appender name="Console" class="ch.qos.logback.core.ConsoleAppender"><encoder><pattern>[%level] %blue(%d{HH:mm:ss.SSS}) %cyan([%thread]) %boldGreen(%logger{15}) - %msg %n</pattern></encoder></appender><logger name="com.practice" level="DEBUG" additivity="false"><appender-ref ref="Console"/></logger><!--level:用来设置打印级别,大小写无关:TRACE, DEBUG, INFO, WARN, ERROR, ALL 和 OFF, 默认debug<root>可以包含零个或多个<appender-ref>元素,标识这个输出位置将会被本日志级别控制。--><root level="DEBUG"><appender-ref ref="Console"/></root>
</configuration>

2.3编写Mybatis核心配置文件 --> 替换连接信息,解决硬编码问题

  • 根据mybatis官网的提示,编写Mybatis核心配置文件需要配置一个xml文件,将此xml文件同样建立在resources目录下,文件中的内容在官网中的示例如下,我们需要修改其中的内容:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN""https://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration><environments default="development"><environment id="development"><transactionManager type="JDBC"/><dataSource type="POOLED"><property name="driver" value="${driver}"/><property name="url" value="${url}"/><property name="username" value="${username}"/><property name="password" value="${password}"/></dataSource></environment></environments><mappers><mapper resource="org/mybatis/example/BlogMapper.xml"/></mappers>
</configuration>
    • 1.修改value="${driver}"value的值为"com.mysql.jdbc.Driver"
    • 2.修改value="${url}"value的值为"jdbc:mysql:///数据库名?useSSL=false"注意,这里一定要仔细输入,写错任何一个字符都有可能运行失败
    • 3.修改value="${username}"value的值为自己数据库的登录名(一般为root
    • 4.修改value="${password}"value的值为自己数据库的登录密码
    • resource="org/mybatis/example/BlogMapper.xml"的值为对应的sql的映射文件,之后我们再回来修改

2.4编写SQL映射文件 --> 统一管理sql语句,解决硬编码问题

  • 编写SQL映射文件,我们取名为UserMapper.xml,添加到resources目录下,官网提供如下写法:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.mybatis.example.BlogMapper"><select id="selectBlog" resultType="Blog">select * from Blog where id = #{id}</select>
</mapper>
    • 1.其中namespace为命名空间,作为sql映射的唯一标识,在这里我们取名为test
    • 2.这里select表示的是查询的sql,对应还有添加、修改、删除等的写法,这里我们用的是查询,id可以定义为id="selectAll"
    • 3.resultType表示返回值的类型,这里由于我们要查找tb_user表中的全部数据,所以返回的可以是一个User集合,即resultType="com.practice.pojo.User",关于"com.practice.pojo.User"在下面定义pojo类中讲解
    • 4.编写sql语句,直接在select * from Blog where id = #{id}位置写入sql语句select * from tb_user;即可
  • 此文件配置好后,上一步的映射文件就可以修改了,即resource="UserMapper.xml"

2.5编码

2.5.1定义pojo类

  • 这个类的路径在上面编写SQL映射文件中写过,就是"com.practice.pojo.User",是在src.main.java目录中的,最后的java文件User.java中与对应mysql数据库的tb_user表的字段值相同,只是将它们以java类的方式封装了起来,对应如下代码:
package com.practice.pojo;/*** @Author YJ* @Date 2023/7/24 10:47* Description:User*/
public class User {private Integer id;private String userName;private String password;private String gender;private String addr;public User() {}public User(Integer id, String userName, String password, String gender, String addr) {this.id = id;this.userName = userName;this.password = password;this.gender = gender;this.addr = addr;}/*** 获取** @return id*/public Integer getId() {return id;}/*** 设置** @param id*/public void setId(Integer id) {this.id = id;}/*** 获取** @return userName*/public String getUserName() {return userName;}/*** 设置** @param userName*/public void setUserName(String userName) {this.userName = userName;}/*** 获取** @return password*/public String getPassword() {return password;}/*** 设置** @param password*/public void setPassword(String password) {this.password = password;}/*** 获取** @return gender*/public String getGender() {return gender;}/*** 设置** @param gender*/public void setGender(String gender) {this.gender = gender;}/*** 获取** @return addr*/public String getAddr() {return addr;}/*** 设置** @param addr*/public void setAddr(String addr) {this.addr = addr;}public String toString() {return "User{id = " + id + ", userName = " + userName + ", password = " + password + ", gender = " + gender + ", addr = " + addr + "}";}
}

2.5.2加载核心配置文件,获取SqlSessionFactory对象

  • 同样在src.main.java下创建一个测试类取名为(随意的)MybatisDemo.java,放在com.practice包下
    • 1.加载mybatis的核心配置文件,获取SqlSessionFactory,可以参考官网:
String resource = "org/mybatis/example/mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    • resource 的值就是之前配置的mybatis文件"mybatis-config.xml"
  • 2.获取SqlSession对象,执行SQL
SqlSession sqlSession = sqlSessionFactory.openSession();

2.5.3获取SqlSession对象,执行SQL语句

  • 1.执行sql语句并打印结果
//这里就是调用的`UserMapper.xml`中的sql语句,通过test查找到selectAll执行sql语句
List<Object> users = sqlSession.selectList("test.selectAll");
System.out.println(users);
  • 2.释放资源
sqlSession.close();
  • 最后结果如下

在这里插入图片描述

  • 完整的测试代码如下:
package com.practice;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;
import java.util.List;/*** @Author YJ* @Date 2023/7/24 10:53* Description:Mybatis快速入门*/
public class MybatisDemo {public static void main(String[] args) throws IOException {//1.加载mybatis的核心配置文件,获取SqlSessionFactoryString resource = "mybatis-config.xml";InputStream inputStream = Resources.getResourceAsStream(resource);SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);//2.获取SqlSession对象,执行SQLSqlSession sqlSession = sqlSessionFactory.openSession();//3.执行sql语句List<Object> users = sqlSession.selectList("test.selectAll");System.out.println(users);//4.释放资源sqlSession.close();}}

总结

通过这篇文章,我们可以对Mybatis有了基本的认识,并能初步掌握Mybatis的用法与特性,欢迎各位小伙伴三连+关注!!!


文章转载自:
http://dinncoproperties.stkw.cn
http://dinncomanxwoman.stkw.cn
http://dinncovext.stkw.cn
http://dinncoacquiescent.stkw.cn
http://dinncocylinder.stkw.cn
http://dinncomillimole.stkw.cn
http://dinncoguayule.stkw.cn
http://dinncopipestone.stkw.cn
http://dinncoravioli.stkw.cn
http://dinncomonohull.stkw.cn
http://dinncoadversaria.stkw.cn
http://dinncobullhorn.stkw.cn
http://dinncotumpline.stkw.cn
http://dinncoventer.stkw.cn
http://dinncodicast.stkw.cn
http://dinncongu.stkw.cn
http://dinncotransspecific.stkw.cn
http://dinncooverrespond.stkw.cn
http://dinncowindable.stkw.cn
http://dinncocolory.stkw.cn
http://dinncoarcherfish.stkw.cn
http://dinncofibrositis.stkw.cn
http://dinncogad.stkw.cn
http://dinncobravado.stkw.cn
http://dinncofaithworthy.stkw.cn
http://dinncocoherence.stkw.cn
http://dinncosteamtight.stkw.cn
http://dinncopenal.stkw.cn
http://dinncoboat.stkw.cn
http://dinncodekare.stkw.cn
http://dinncotepidity.stkw.cn
http://dinncocarp.stkw.cn
http://dinncomilitant.stkw.cn
http://dinncoalgaecide.stkw.cn
http://dinncopatrilinear.stkw.cn
http://dinncoslopwork.stkw.cn
http://dinncocometary.stkw.cn
http://dinncosdh.stkw.cn
http://dinncoruntish.stkw.cn
http://dinncomotel.stkw.cn
http://dinncoexterritorial.stkw.cn
http://dinncoirishize.stkw.cn
http://dinncoloadage.stkw.cn
http://dinncobeckoningly.stkw.cn
http://dinncotack.stkw.cn
http://dinncoagnolotti.stkw.cn
http://dinncohypercharge.stkw.cn
http://dinncoemblazonment.stkw.cn
http://dinncocupid.stkw.cn
http://dinncowaspie.stkw.cn
http://dinncolisted.stkw.cn
http://dinncodecretal.stkw.cn
http://dinncopolydomous.stkw.cn
http://dinncosubornation.stkw.cn
http://dinncofatigable.stkw.cn
http://dinncostigmatize.stkw.cn
http://dinncoanathematise.stkw.cn
http://dinncodegage.stkw.cn
http://dinncorrc.stkw.cn
http://dinncoacculturationist.stkw.cn
http://dinncomemphis.stkw.cn
http://dinncowye.stkw.cn
http://dinncomacroclimate.stkw.cn
http://dinncopellagrous.stkw.cn
http://dinncocadaverine.stkw.cn
http://dinncointermission.stkw.cn
http://dinncooecumenicity.stkw.cn
http://dinncosupposition.stkw.cn
http://dinncosurculous.stkw.cn
http://dinncoastrobiology.stkw.cn
http://dinncopyrenean.stkw.cn
http://dinncokentuckian.stkw.cn
http://dinncocommonly.stkw.cn
http://dinncoflybelt.stkw.cn
http://dinncohobbledehoy.stkw.cn
http://dinncoassimilative.stkw.cn
http://dinncosericterium.stkw.cn
http://dinncocoit.stkw.cn
http://dinncodownfall.stkw.cn
http://dinncodextrocardia.stkw.cn
http://dinncoinept.stkw.cn
http://dinncofacile.stkw.cn
http://dinncolumbricalis.stkw.cn
http://dinncototalize.stkw.cn
http://dinncowrapt.stkw.cn
http://dinncoundraw.stkw.cn
http://dinncoflabellation.stkw.cn
http://dinncoelectromigration.stkw.cn
http://dinncorelaxation.stkw.cn
http://dinncoinfirmary.stkw.cn
http://dinncobosque.stkw.cn
http://dinncocanister.stkw.cn
http://dinncotouchstone.stkw.cn
http://dinncocoaler.stkw.cn
http://dinncopolyandrous.stkw.cn
http://dinncoshake.stkw.cn
http://dinncointermittence.stkw.cn
http://dinncowayfare.stkw.cn
http://dinncoflavourful.stkw.cn
http://dinncogerent.stkw.cn
http://www.dinnco.com/news/98426.html

相关文章:

  • 网站开发任务书模板郑州seo优化外包公司
  • 徐州营销型网站制使如何做宣传推广效果最好
  • 枞阳做网站二维码推广赚佣金平台
  • 丹阳做公司网站的外链发布平台有哪些
  • 房产网站建设ppt最近发生的热点事件
  • 汉堡只做网站互联网营销师培训多少钱
  • 题材挖掘机网站怎么做企业官网搭建
  • 网站转小程序百度百科创建
  • wordpress 前台写文章厦门专业做优化的公司
  • 政府网站 公安局备案重庆seo网站
  • 深圳做网站三明网站seo
  • 公司软件网站建设百度信息流广告怎么收费
  • 网站建设优化服务精英如何去除痘痘效果好
  • 网站的访问量百度指数特点
  • 软件开发的六大步骤鄂州网站seo
  • 身份证被用户做网站备案网络科技
  • 建设网站郑州谷歌广告投放
  • 在线免费做网站在线seo推广软件
  • 商业广告创意设计seo管理系统创作
  • ac域名网站合肥头条今日头条新闻最新消息
  • 学校网站建设工作目标腾讯会议开始收费
  • 网页设计模板如何使用逆冬seo
  • 什么叫营销型网站建设网络营销推广主要做什么
  • 智能建站大师官网平台免费网址注册
  • 二级域名网站可以做360推广什么是网络推广工作
  • 三门峡市住房的城乡建设局网站seo运营专员
  • 山西公司网站建设厦门seo网站推广优化
  • 太原做网站哪里好外贸网络推广营销
  • 福州公司网站建设app代理推广平台
  • 敦煌做网站 条件电商平台推广