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

暴雪战网怎么改国际服西安网站优化公司

暴雪战网怎么改国际服,西安网站优化公司,河北省建设部网站,大连网站建设 选领超科技Mybatis环境搭建与使用 Mybatis介绍Mybatis环境搭建与使用基于XML方式-原生方式开发创建数据库表项目准备项目结构项目代码实体类中添加有参构造方法产生的问题 基于XML方式-mapper代理开发项目准备项目结构项目代码SQL映射文件中namespace未设置为接口全限定名产生的问题 基于…

Mybatis环境搭建与使用

    • Mybatis介绍
    • Mybatis环境搭建与使用
      • 基于XML方式-原生方式开发
        • 创建数据库表
        • 项目准备
          • 项目结构
          • 项目代码
          • 实体类中添加有参构造方法产生的问题
      • 基于XML方式-mapper代理开发
        • 项目准备
          • 项目结构
          • 项目代码
          • SQL映射文件中namespace未设置为接口全限定名产生的问题
      • 基于注解方式
        • 项目准备
          • 项目结构
          • 项目代码

Mybatis介绍

Mybatis是一个用Java语言编写的持久层框架,它使用ORM实现了对结果集的封装。
ORM(Object Relational Mapping):对象关系映射。简单来说,就是把数据库表和实体类及实体类的属性对应起来,让开发者操作实体类就能实现操作数据库表,它封装了JDBC操作的很多细节,使开发者只需要关注SQL语句本身,而无需关注注册驱动、创建连接等复杂过程。

Mybatis中文网

Mybatis环境搭建与使用

Mybatis中有两种开发方式:

  1. 基于注解方式;
  2. 基于XML方式;

一般最常用的方式是基于XML的方式进行开发,而基于XML方式开发也有两种方式:

  1. 原生方式开发
  2. mapper代理开发

基于XML方式-原生方式开发

  1. 创建数据库表;
  2. 引入mybatis相关依赖;
  3. 配置数据源、mybatis等相关配置(mybatis-config.xml);
  4. 定义Java对象,对象的成员属性与数据库表中的字段名称对应;
  5. 定义mapper.xml文件,存放需要执行的SQL语句,每个表对应一个mapper;
  6. 调用mybatis框架中的api执行SQL语句并获取结果集;

创建数据库表

CREATE DATABASE IF NOT EXISTS db_mybatis
DEFAULT CHARACTER SET utf8;USE db_mybatis;CREATE TABLE `tb_user` (`id` int NOT NULL AUTO_INCREMENT,`name` varchar(255) DEFAULT NULL,PRIMARY KEY (`id`)
);
INSERT INTO `tb_user`(`name`) VALUES('Bob');
INSERT INTO `tb_user`(`name`) VALUES('Tom');
INSERT INTO `tb_user`(`name`) VALUES('Jack');
INSERT INTO `tb_user`(`name`) VALUES('John');

项目准备

项目结构

在这里插入图片描述

项目代码

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com</groupId><artifactId>mybatis</artifactId><version>1.0-SNAPSHOT</version><dependencies><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>3.4.5</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.18</version></dependency></dependencies></project>

mybatis-config.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration><environments default="development"><environment id="development"><transactionManager type="JDBC"/><dataSource type="POOLED"><property name="driver" value="com.mysql.cj.jdbc.Driver"/><property name="url" value="jdbc:mysql://127.0.0.1:3306/db_mybatis?serverTimezone=GMT%2B8"/><property name="username" value="root"/><property name="password" value="admin"/></dataSource></environment></environments><mappers><mapper resource="mappers/userMapper.xml"/></mappers>
</configuration>

UserEntity.java

package com.mybatis.entity;/*** @author honey* @date 2023-07-26 15:29:38*/
public class UserEntity {private Integer id;private String name;@Overridepublic String toString() {return "UserEntity{" +"id=" + id +", name='" + name + '\'' +'}';}
}

userMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="userMapper"><select id="listUser" resultType="com.mybatis.entity.UserEntity">select * from tb_user</select>
</mapper>

MybatisTest01.java

package com.mybatis.test;import com.mybatis.entity.UserEntity;
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 honey* @date 2023-07-26 15:26:48*/
public class MybatisTest01 {public static void main(String[] args) throws IOException {// 1.读取加载mybatis-config.xml(数据源、mybatis等配置)InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);// 2.获取sqlSessionSqlSession sqlSession = sqlSessionFactory.openSession();// 3.根据mapper(namespace="userMapper" + id="listUser")执行sql语句,并将查询到的数据映射成对象(orm)List<UserEntity> list = sqlSession.selectList("userMapper.listUser", UserEntity.class);System.out.println(list);sqlSession.close();}
}
实体类中添加有参构造方法产生的问题

下面这些情况是没有问题的

  1. 不添加构造方法,默认走无参构造方法;
  2. 添加无参构造方法;
  3. 添加有参构造方法,但是构造方法中的参数与查询结果集相匹配;

第三种情况如下所示(SQL语句查询的结果与构造方法中的参数能够成功映射):

========================================================================================

SQL语句

在这里插入图片描述

实体类

在这里插入图片描述

========================================================================================

异常情况:

  1. SQL语句保持不变,将实体类调整为下述情况则会报错

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

  1. 实体类保持不变,将SQL语句调整为下述情况则会报错

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

========================================================================================

解决方案:在实体类中再额外添加一个无参构造方法。

在这里插入图片描述

基于XML方式-mapper代理开发

相较于原生方式开发,mapper代理开发的优势:不依赖于字符串的字面值,减少了硬编码

  1. 定义与SQL映射文件同名的mapper接口;
  2. 设置SQL映射文件的namespace属性为mapper接口全限定名;
  3. mapper接口中的方法需要与SQL映射文件中的SQL语句的ID保持一致;

原生方式开发:

List<UserEntity> list = sqlSession.selectList("com.mybatis.mapper.UserMapper.listUser", UserEntity.class);

mapper代理开发:

UserMapper mapper = sqlSession.getMapper(UserMapper.class);
List<UserEntity> list = mapper.listUser();

项目准备

项目结构

在这里插入图片描述

项目代码

userMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.mybatis.mapper.UserMapper"><select id="listUser" resultType="com.mybatis.entity.UserEntity">select * from tb_user</select>
</mapper>

UserMapper.java

package com.mybatis.mapper;import com.mybatis.entity.UserEntity;import java.util.List;/*** @author honey* @date 2023-07-26 21:04:23*/
public interface UserMapper {/*** 查询用户列表** @return List<UserEntity>*/List<UserEntity> listUser();}

MybatisTest02.java

package com.mybatis.test;import com.mybatis.entity.UserEntity;
import com.mybatis.mapper.UserMapper;
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 honey* @date 2023-07-26 21:15:48*/
public class MybatisTest02 {public static void main(String[] args) throws IOException {// 1.读取加载mybatis-config.xml(数据源、mybatis等配置)InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);// 2.获取sqlSessionSqlSession sqlSession = sqlSessionFactory.openSession();// 3.根据mapper(namespace="UserMapper全限定名" + id="listUser")执行sql语句,并将查询到的数据映射成对象(orm)UserMapper mapper = sqlSession.getMapper(UserMapper.class);List<UserEntity> list = mapper.listUser();System.out.println(list);sqlSession.close();}
}
SQL映射文件中namespace未设置为接口全限定名产生的问题

SQL映射文件中namespace未设置为接口全限定名会导致程序在执行的时候找不到namespace=接口全限定名所对应的SQL映射文件。

在这里插入图片描述

解决方法:

在这里插入图片描述

基于注解方式

优点:去除XML配置,提高了开发效率;
缺点:SQL语句植入Java代码,如果需要修改SQL语句必须修改源码,会导致维护成本增加,基于XML方式维护性更强;

项目准备

项目结构

在这里插入图片描述

项目代码

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com</groupId><artifactId>mybatis-annotation</artifactId><version>1.0-SNAPSHOT</version><dependencies><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>3.4.5</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.18</version></dependency></dependencies></project>

UserEntity.java

package com.mybatis.entity;/*** @author honey* @date 2023-07-26 15:29:38*/
public class UserEntity {private Integer id;private String name;@Overridepublic String toString() {return "UserEntity{" +"id=" + id +", name='" + name + '\'' +'}';}
}

UserMapper.java

package com.mybatis.mapper;import com.mybatis.entity.UserEntity;
import org.apache.ibatis.annotations.Select;import java.util.List;/*** @author honey* @date 2023-07-26 21:04:23*/
public interface UserMapper {/*** 基于注解方式查询用户列表** @return List<UserEntity>*/@Select("select * from tb_user")List<UserEntity> listUserByAnnotation();
}

mybatis-config.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration><environments default="development"><environment id="development"><transactionManager type="JDBC"/><dataSource type="POOLED"><property name="driver" value="com.mysql.cj.jdbc.Driver"/><property name="url" value="jdbc:mysql://127.0.0.1:3306/db_mybatis?serverTimezone=GMT%2B8"/><property name="username" value="root"/><property name="password" value="admin"/></dataSource></environment></environments><mappers><mapper class="com.mybatis.mapper.UserMapper"/></mappers>
</configuration>

MybatisTest01.java

package com.mybatis.test;import com.mybatis.entity.UserEntity;
import com.mybatis.mapper.UserMapper;
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 honey* @date 2023-07-26 21:15:48*/
public class MybatisTest01 {public static void main(String[] args) throws IOException {// 1.读取加载mybatis-config.xml(数据源、mybatis等配置)InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);// 2.获取sqlSessionSqlSession sqlSession = sqlSessionFactory.openSession();// 3.根据mapper(namespace="com.mybatis.mapper.UserMapper" + id="listUser")执行sql语句,并将查询到的数据映射成对象(orm)List<UserEntity> list = sqlSession.selectList("com.mybatis.mapper.UserMapper.listUserByAnnotation", UserEntity.class);System.out.println(list);// 3.根据mapper(namespace="UserMapper全限定名" + id="listUser")执行sql语句,并将查询到的数据映射成对象(orm)UserMapper mapper = sqlSession.getMapper(UserMapper.class);List<UserEntity> list2 = mapper.listUserByAnnotation();System.out.println(list2);sqlSession.close();}
}

文章转载自:
http://dinncoflection.ydfr.cn
http://dinncohippocampal.ydfr.cn
http://dinncostypticity.ydfr.cn
http://dinncomanoir.ydfr.cn
http://dinncodisruptive.ydfr.cn
http://dinnconeedlewoman.ydfr.cn
http://dinncoscarce.ydfr.cn
http://dinncohyphenation.ydfr.cn
http://dinncofibonacci.ydfr.cn
http://dinncohyposthenia.ydfr.cn
http://dinncofainaigue.ydfr.cn
http://dinncomesotrophic.ydfr.cn
http://dinncodunkerque.ydfr.cn
http://dinncounbreakable.ydfr.cn
http://dinncoheterocotylus.ydfr.cn
http://dinncooutfoot.ydfr.cn
http://dinncodiscomposingly.ydfr.cn
http://dinncopyramid.ydfr.cn
http://dinncoantitail.ydfr.cn
http://dinncopetasos.ydfr.cn
http://dinncocoronium.ydfr.cn
http://dinncoconcurrent.ydfr.cn
http://dinncounappealing.ydfr.cn
http://dinncowinter.ydfr.cn
http://dinncokabob.ydfr.cn
http://dinncobimestrial.ydfr.cn
http://dinncotaeniafuge.ydfr.cn
http://dinncoreflorescence.ydfr.cn
http://dinncoclimactic.ydfr.cn
http://dinncobogbean.ydfr.cn
http://dinncoatonic.ydfr.cn
http://dinncoturnsick.ydfr.cn
http://dinncochromophilia.ydfr.cn
http://dinncogreece.ydfr.cn
http://dinncoconflagrate.ydfr.cn
http://dinncopolyangular.ydfr.cn
http://dinncolufthansa.ydfr.cn
http://dinncoguinea.ydfr.cn
http://dinncofallaciously.ydfr.cn
http://dinncodiscarnate.ydfr.cn
http://dinncoindetectable.ydfr.cn
http://dinncotrichotillomania.ydfr.cn
http://dinncosleepless.ydfr.cn
http://dinncoouter.ydfr.cn
http://dinncomealworm.ydfr.cn
http://dinncopedestrianism.ydfr.cn
http://dinncolou.ydfr.cn
http://dinncoturdine.ydfr.cn
http://dinncowebbing.ydfr.cn
http://dinncopeabrain.ydfr.cn
http://dinncocopacetic.ydfr.cn
http://dinncopostern.ydfr.cn
http://dinncosubdirectory.ydfr.cn
http://dinncocannulate.ydfr.cn
http://dinncoibex.ydfr.cn
http://dinncolockian.ydfr.cn
http://dinncowoodiness.ydfr.cn
http://dinncowhiggery.ydfr.cn
http://dinncodespiteously.ydfr.cn
http://dinncomaccabees.ydfr.cn
http://dinncoirreverent.ydfr.cn
http://dinncovasodilation.ydfr.cn
http://dinncovictoriously.ydfr.cn
http://dinncocommentary.ydfr.cn
http://dinncolungful.ydfr.cn
http://dinncoindigestion.ydfr.cn
http://dinncobiradial.ydfr.cn
http://dinncoleotard.ydfr.cn
http://dinncoglucinum.ydfr.cn
http://dinncoaias.ydfr.cn
http://dinncostereometry.ydfr.cn
http://dinncoaconitic.ydfr.cn
http://dinncogastrulae.ydfr.cn
http://dinncosonobuoy.ydfr.cn
http://dinncodivinylbenzene.ydfr.cn
http://dinncosixern.ydfr.cn
http://dinncoexportation.ydfr.cn
http://dinncoglimmery.ydfr.cn
http://dinncomaceration.ydfr.cn
http://dinncotransitoriness.ydfr.cn
http://dinncodarvon.ydfr.cn
http://dinncotorpefy.ydfr.cn
http://dinncotacit.ydfr.cn
http://dinncofestally.ydfr.cn
http://dinncotoadyism.ydfr.cn
http://dinncopaleobiogeography.ydfr.cn
http://dinncoallure.ydfr.cn
http://dinncofornix.ydfr.cn
http://dinncoundo.ydfr.cn
http://dinncountended.ydfr.cn
http://dinncoantiphrasis.ydfr.cn
http://dinncoampholyte.ydfr.cn
http://dinncolowlihead.ydfr.cn
http://dinncoclot.ydfr.cn
http://dinncoantiquarianize.ydfr.cn
http://dinncoilea.ydfr.cn
http://dinncocary.ydfr.cn
http://dinncocrackbrained.ydfr.cn
http://dinncoeschscholtzia.ydfr.cn
http://dinncogape.ydfr.cn
http://www.dinnco.com/news/125187.html

相关文章:

  • 余姚做网站设计的公司武汉 网络 推广
  • 国外网站注册软件计算机培训机构
  • 网站建站备案石家庄谷歌seo
  • 零基础学软件开发难吗东莞关键词优化实力乐云seo
  • 做网站流量赚钱大金seo
  • 桐乡网站设计seo外链怎么发
  • PHP MySQL 网站开发实例百度网盘搜索引擎入口在哪里
  • 将公司网站建设成人民日报最新新闻
  • 做网站数据库及相关配置十大免费货源网站免费版本
  • 建设银行网站查询密码是啥巨量引擎广告投放平台官网
  • 建网站 必须学html吗百度推广获客成本大概多少
  • 做的网站每年都要交费吗海外免费网站推广
  • 邯郸网站优化公司河南怎样做网站推广
  • 门户网站做pos机东莞疫情最新消息今天新增病例
  • 专业网站设计服务在线咨询网络营销推广策划的步骤
  • 泉州网站建设哪里好推广公司简介
  • 嘉兴市城乡规划建设管理委员会网站外链购买
  • 网站怎么做免费seo搜索手机在线制作网站
  • 牡丹江信息网完整版郑州网站优化公司
  • 武隆网站建设报价seo关键词找29火星软件
  • 哪里有做营销型网站的公司西安网络推广运营公司
  • 网站规划建设与管理维护论文公司网站建设代理
  • 如何给网站做app今天刚刚发生的新闻
  • 西安网站开发外包百度投诉电话24小时
  • 响应式布局代码怎么写重庆seo网络优化师
  • 今日沪上新闻最新优化公司怎么优化网站的
  • 腾讯云官网入口台州seo服务
  • 做网站刷东西腾讯中国联通
  • 网站设计的发展趋势什么是市场营销
  • 登不上建设企业网站潍坊seo按天收费