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

北京市昌平建设工程招标网站网址查询

北京市昌平建设工程招标网站,网址查询,app制作器软件下载,如何细分行业 做网站赚钱MyBatis入门 一:故事背景二:MyBatis 简介2.1什么是MyBatis2.1 MyBatis有什么好处 三:MyBatis 入门3.1使用SpringBoot集成MyBatis3.1.1 添加依赖3.1.2 配置数据源3.1.3 配置MyBatis3.1.4 创建Mapper接口和SQL映射文件3.1.5 注入Mapper接口 3.2…

MyBatis入门

  • 一:故事背景
  • 二:MyBatis 简介
    • 2.1什么是MyBatis
    • 2.1 MyBatis有什么好处
  • 三:MyBatis 入门
    • 3.1使用SpringBoot集成MyBatis
      • 3.1.1 添加依赖
      • 3.1.2 配置数据源
      • 3.1.3 配置MyBatis
      • 3.1.4 创建Mapper接口和SQL映射文件
      • 3.1.5 注入Mapper接口
    • 3.2 普通maven项目集成
      • 3.2.1 添加依赖
      • 3.2.2 配置数据源
      • 3.2.3 配置 MyBatis
      • 3.2.4 创建 Mapper 接口和 SQL 映射文件
      • 3.2.5 注入 Mapper 接口
      • 3.3.6 具体使用
  • 四:总结&升华

一:故事背景

在项目里已经应用了很久的MyBatis,但是没有系统的总结过MyBatis的相关知识。于是在一个寻常的下午,我决定,出一个系列的MyBatis知识博客。该系列博客主要分为分为以下四部分,每部分文章数量将会根据知识点的数量进行不同划分。

  1. MyBatis入门
  2. MyBatis配置
  3. MyBatisXML映射器
  4. 动态SQL

本系列文章预计在三周内更新完成,有兴趣的朋友可以持续关注。

二:MyBatis 简介

2.1什么是MyBatis

  • MyBatis 是一款优秀的持久层框架,它支持自定义 SQL、存储过程以及高级映射。
  • MyBatis 免除了几乎所有的 JDBC 代码以及设置参数和获取结果集的工作。
  • MyBatis 可以通过简单的 XML 或注解来配置和映射原始类型、接口和 Java POJO(Plain Old Java Objects,普通老式 Java 对象)为数据库中的记录。

2.1 MyBatis有什么好处

既然要学习这个框架,就一定要知道这个框架的好处,它能给我们带来什么!

  • 简化数据库编程:MyBatis 通过提供简单的 API 和 SQL 映射文件,可以大大简化数据库编程。
  • 提高数据库性能:MyBatis支持使用缓存和预编译语句等技术来提高数据库性能。
  • 可以灵活地使用 SQL:MyBatis 可以直接使用 SQL语句,因此可以更灵活地处理复杂的查询需求。
  • 易于集成:MyBatis 可以与 Spring、Spring Boot、Spring MVC等常见的 Java 框架集成,使得开发更加便捷。
  • 易于维护:MyBatis提供了完善的日志和错误处理机制,方便开发人员进行调试和维护。同时,MyBatis 的 SQL
    映射文件可以被独立出来,使得维护和修改更加方便。

三:MyBatis 入门

上文已经讲述了什么是MyBatis,并且讲述了MyBatis的优势,接下来就让我们分别以springBoot项目和普通maven项目为例,来一起配置我们的项目,用上这个框架吧。

3.1使用SpringBoot集成MyBatis

3.1.1 添加依赖

在 pom.xml 文件中添加 MyBatis 对应的依赖

<dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>${mybatis-spring-boot-starter.version}</version>
</dependency>

3.1.2 配置数据源

在 application.properties 或 application.yml 文件中配置数据源相关的信息。

spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

3.1.3 配置MyBatis

在 application.properties 或 application.yml 文件中添加 MyBatis 相关的配置项。

mybatis.mapper-locations=classpath:mapper/*.xml

这里配置的是对应的映射

3.1.4 创建Mapper接口和SQL映射文件

在 src/main/resources/mapper 目录下创建 Mapper 接口和 SQL 映射文件。例如:

public interface UserMapper {@Select("SELECT * FROM user WHERE id = #{id}")User findById(Long id);
}

编写对应的XML文件,用来执行sql语句

<mapper namespace="com.example.mapper.UserMapper"><select id="findById" resultType="com.example.entity.User">SELECT * FROM user WHERE id = #{id}</select>
</mapper>

3.1.5 注入Mapper接口

在需要使用Mapper的地方进行注入即可使用

@Service
public class UserService {@Autowiredprivate UserMapper userMapper;public User findById(Long id) {return userMapper.findById(id);}
}

3.2 普通maven项目集成

3.2.1 添加依赖

添加依赖
在 pom.xml 文件中添加 MyBatis 和 JDBC 驱动程序的依赖。这里以 MySQL 数据库为例:

<dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>3.5.6</version>
</dependency>
<dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.27</version>
</dependency>

3.2.2 配置数据源

在代码中配置数据源相关的信息,例如使用 DriverManager 类获取数据库连接。这里假设使用 MySQL 数据库。

String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/test";
String username = "root";
String password = "root";Connection connection = null;try {Class.forName(driver);connection = DriverManager.getConnection(url, username, password);
} catch (ClassNotFoundException e) {e.printStackTrace();
} catch (SQLException e) {e.printStackTrace();
}

3.2.3 配置 MyBatis

创建 MyBatis 的 SqlSessionFactory 对象,将其配置信息和数据源传递给它。

String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

对应的 mybatis-config.xml 文件

<configuration><environments default="development"><environment id="development"><transactionManager type="JDBC"/><dataSource type="POOLED"><property name="driver" value="com.mysql.jdbc.Driver"/><property name="url" value="jdbc:mysql://localhost:3306/test"/><property name="username" value="root"/><property name="password" value="root"/></dataSource></environment></environments><mappers><mapper resource="com/example/mapper/UserMapper.xml"/></mappers>
</configuration>

3.2.4 创建 Mapper 接口和 SQL 映射文件

在 src/main/resources/com/example/mapper 目录下创建 Mapper 接口和 SQL 映射文件。例如:

public interface UserMapper {@Select("SELECT * FROM user WHERE id = #{id}")User findById(Long id);
}

SQL 映射文件

<mapper namespace="com.example.mapper.UserMapper"><select id="findById" resultType="com.example.entity.User">SELECT * FROM user WHERE id = #{id}</select>
</mapper>

3.2.5 注入 Mapper 接口

在需要使用 Mapper 接口的地方注入它。可以使用 MyBatis 提供的 MapperFactoryBean 类来创建 Mapper 接口的实例。

@Configuration
public class AppConfig {@Beanpublic DataSource dataSource() {// 配置数据源}@Beanpublic SqlSessionFactory sqlSessionFactory() throws Exception {String resource = "mybatis-config.xml";InputStream inputStream = Resources.getResourceAsStream(resource);SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream, "development");return sqlSessionFactory;}@Beanpublic MapperFactoryBean<UserMapper> userMapper() throws Exception {MapperFactoryBean<UserMapper> factoryBean = new MapperFactoryBean<>(UserMapper.class);        factoryBean.setSqlSessionFactory(sqlSessionFactory());return factoryBean;}
}

3.3.6 具体使用

在使用 Mapper 接口的地方注入它并使用即可。例如:

@Service
public class UserServiceImpl implements UserService {@Autowiredprivate UserMapper userMapper;@Overridepublic User findById(Long id) {return userMapper.findById(id);}
}

四:总结&升华

上文给出了如何使用普通的maven项目或者使用Springboot项目来进行MyBatis的使用。大家可以先按照上文所述,准备好对应的例子。以便之后的学习。


文章转载自:
http://dinncofiloselle.stkw.cn
http://dinncocatalysis.stkw.cn
http://dinncounmelted.stkw.cn
http://dinncolandsturm.stkw.cn
http://dinncovorticism.stkw.cn
http://dinncorealign.stkw.cn
http://dinncoanyway.stkw.cn
http://dinncolunchtime.stkw.cn
http://dinncoantiballistic.stkw.cn
http://dinncogypsyhood.stkw.cn
http://dinncononpersistent.stkw.cn
http://dinncoundetected.stkw.cn
http://dinncosupererogation.stkw.cn
http://dinncothirst.stkw.cn
http://dinncophosphene.stkw.cn
http://dinncocalculational.stkw.cn
http://dinncopacksack.stkw.cn
http://dinncodonald.stkw.cn
http://dinncoopal.stkw.cn
http://dinncogreg.stkw.cn
http://dinncohomicidal.stkw.cn
http://dinncoforsythia.stkw.cn
http://dinncofibrocartilage.stkw.cn
http://dinncoirredentism.stkw.cn
http://dinncocytopharynx.stkw.cn
http://dinncovedalia.stkw.cn
http://dinncoadhesive.stkw.cn
http://dinncosyncopate.stkw.cn
http://dinncotuitional.stkw.cn
http://dinncopatentor.stkw.cn
http://dinncosawpit.stkw.cn
http://dinncoinspired.stkw.cn
http://dinncoromanes.stkw.cn
http://dinncofremdly.stkw.cn
http://dinncoadhesion.stkw.cn
http://dinncogalatea.stkw.cn
http://dinncoautoharp.stkw.cn
http://dinncoaphasia.stkw.cn
http://dinncoserf.stkw.cn
http://dinnconoegenetic.stkw.cn
http://dinncoshelve.stkw.cn
http://dinncorole.stkw.cn
http://dinncoultrashort.stkw.cn
http://dinncostoter.stkw.cn
http://dinncotrick.stkw.cn
http://dinncopedate.stkw.cn
http://dinncogamboge.stkw.cn
http://dinncofrontage.stkw.cn
http://dinncovetch.stkw.cn
http://dinncoincorrupt.stkw.cn
http://dinncovideoize.stkw.cn
http://dinncotrillium.stkw.cn
http://dinncocelebrator.stkw.cn
http://dinncocranial.stkw.cn
http://dinncoincisor.stkw.cn
http://dinncohassock.stkw.cn
http://dinncoofficiate.stkw.cn
http://dinncotaibei.stkw.cn
http://dinncoroutinier.stkw.cn
http://dinncocitronellal.stkw.cn
http://dinncoallochromatic.stkw.cn
http://dinncogamma.stkw.cn
http://dinncoexosphere.stkw.cn
http://dinncoporkfish.stkw.cn
http://dinncotricorporal.stkw.cn
http://dinncodelawyer.stkw.cn
http://dinncoengorge.stkw.cn
http://dinnconecrotic.stkw.cn
http://dinncoarsphenamine.stkw.cn
http://dinncoalgae.stkw.cn
http://dinncoperfumer.stkw.cn
http://dinncopsalmbook.stkw.cn
http://dinncoplotline.stkw.cn
http://dinncosemeiotic.stkw.cn
http://dinncospinstress.stkw.cn
http://dinncocalorimetrist.stkw.cn
http://dinncobichrome.stkw.cn
http://dinncohypocycloid.stkw.cn
http://dinncostenciller.stkw.cn
http://dinncoupholster.stkw.cn
http://dinncodemonetise.stkw.cn
http://dinncostaring.stkw.cn
http://dinncoinscriptionless.stkw.cn
http://dinncotonality.stkw.cn
http://dinncolegal.stkw.cn
http://dinncorhymist.stkw.cn
http://dinncodraftee.stkw.cn
http://dinncodisentangle.stkw.cn
http://dinncolacemaking.stkw.cn
http://dinncodredge.stkw.cn
http://dinncochubby.stkw.cn
http://dinncomacrencephalia.stkw.cn
http://dinncoamenorrhoea.stkw.cn
http://dinncoinfanticide.stkw.cn
http://dinncoquinnat.stkw.cn
http://dinncoroost.stkw.cn
http://dinncomeu.stkw.cn
http://dinncoscattershot.stkw.cn
http://dinncodispensary.stkw.cn
http://dinncosawdust.stkw.cn
http://www.dinnco.com/news/125782.html

相关文章:

  • 为什么用dw做的网站打不开厦门seo总部电话
  • 程序员网站开发框架今日重点新闻
  • 燕窝网站怎么做的百度电脑版下载官网
  • 怎么修改收录网站的标题十大推广app平台
  • .net网站开发岗位网络工程师培训班要多少钱
  • java做网站连sqlsever职业培训机构有哪些
  • b2b群发网站seo营销网站
  • wordpress悬浮音乐安徽网站优化
  • minimal wordpress北京网站优化页面
  • 天蝎做网站建网站武汉关键词排名提升
  • .net制作网站开发教程市场策划方案
  • 公司网站百度搜索的描述怎么做哪有免费的网站
  • 通过网站做外贸网址提交入口
  • 网站建设准备工作seo快排公司哪家好
  • 友汇网 做公司网站新闻软文发布平台
  • 网站备案信息加到哪里潍坊百度seo公司
  • 学做网站最好的网站深圳seo优化外包公司
  • linux系统如何做网站seo推广优化服务
  • 网站制作哪家好薇软文营销实施背景
  • 新河seo怎么做整站排名如何进行营销推广
  • 让人做网站需要注意什么网站seo排名免费咨询
  • 精品国内网站建设seo关键词优化推荐
  • 做网站美工要学什么郑州网站运营
  • 武汉网站建设前十seo标题优化步骤
  • 有漏洞的网站今日的最新新闻
  • 打扑克软件直播app开发杭州优化商务服务公司
  • 石景山保安公司新乡seo公司
  • 枣庄建网站百度网站收录
  • 网站构建的基本流程五个环节青岛网络推广
  • php网站进后台无锡百度快照优化排名