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

深圳市专业的做网站淄博网站制作优化

深圳市专业的做网站,淄博网站制作优化,ps网页模板,网络营销的主要手段上一篇我们介绍了在pom文件中引入mybatis依赖,配置了mybatis配置文件,通过读取配置文件创建了会话工厂,使用会话工厂创建会话获取连接对象读取到了数据库的基本信息。 如果您需要对上面的内容进行了解,可以参考Mybatis引入与使用…

上一篇我们介绍了在pom文件中引入mybatis依赖,配置了mybatis配置文件,通过读取配置文件创建了会话工厂,使用会话工厂创建会话获取连接对象读取到了数据库的基本信息。

如果您需要对上面的内容进行了解,可以参考Mybatis引入与使用https://blog.csdn.net/m1729339749/article/details/132457971

 本篇我们在上一篇的基础上了解如果使用mybatis查询数据:

一、准备数据

这里我们直接使用脚本初始化数据库中的数据

-- 如果数据库不存在则创建数据库
CREATE DATABASE IF NOT EXISTS demo DEFAULT CHARSET utf8;
-- 切换数据库
USE demo;
-- 创建用户表
CREATE TABLE IF NOT EXISTS T_USER(ID INT PRIMARY KEY,USERNAME VARCHAR(32) NOT NULL,AGE INT NOT NULL 
);
-- 插入用户数据
INSERT INTO T_USER(ID, USERNAME, AGE)
VALUES(1, '张三', 20),(2, '李四', 22),(3, '王五', 24);

创建了一个名称为demo的数据库;并在库里创建了名称为T_USER的用户表并向表中插入了数据

二、创建用户实体类

在cn.horse.demo2包下创建UserInfo实体类,为了方便打印用户的信息这里重写了ToString()方法

package cn.horse.demo2;public class UserInfo {private int id;private String username;private int age;@Overridepublic String toString() {StringBuilder result = new StringBuilder();result.append('[');result.append("id: " + this.id);result.append(", ");result.append("username: " + this.username);result.append(", ");result.append("age: " + this.age);result.append(']');return result.toString();}
}

三、配置Mapper文件

这里我在resources下创建了一个demo2的目录,并在目录下创建了一个UserInfoMapper.xml的配置文件。

<?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="UserInfoMapper"><select id="findById" resultType="cn.horse.demo2.UserInfo">SELECT *FROM T_USERWHERE ID = #{id}</select><select id="findAll" resultType="cn.horse.demo2.UserInfo">SELECT *FROM T_USER</select>
</mapper>

namespace代表的是名称空间;如果有多个Mapper配置文件,名称空间不允许重复

select查询标签;

        id代表的是编号,同一个名称空间下编号不允许重复;

       resultType代表的是结果类型,指将数据库查询出来的数据转换成结果类型对象,转换的前提是结果类型需要提供无参数的构造方法(用于创建结果类型对象),数据库中的列需要与结果类型中的字段一一对应(使用反射的方式进行对象中字段的赋值),否则无法创建结果类型对象或者无法将数据绑定到结果类型对象的字段上。

        标签内容是SQL语句,代表使用ID查询用户信息,其中#{id}是参数;在执行当前查询语句时需要携带此参数。

四、引入Mapper配置

Mapper配置完成后,并没有生效;需要在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="${driver}"/><property name="url" value="jdbc:mysql://${ip}:${port}/${database}?useUnicode=true&amp;useSSL=false&amp;characterEncoding=utf8"/><property name="username" value="${username}"/><property name="password" value="${password}"/></dataSource></environment></environments><mappers><mapper resource="demo2/UserInfoMapper.xml" /></mappers>
</configuration>

其中mapper标签是我们引入的配置,resource用于指向Mapper配置文件的位置。

五、查询一条数据

// 读取mybatis配置文件
InputStream inputStream = ClassLoader.getSystemClassLoader().getResourceAsStream("mybatis-config.xml");
// 根据配置创建SqlSession工厂
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);SqlSession sqlSession = null;
try {// 创建SqlSessionsqlSession = sqlSessionFactory.openSession();// 根据用户编号查询一条用户信息UserInfo userInfo = sqlSession.selectOne("UserInfoMapper.findById", 2);System.out.println(userInfo);
} finally {// 关闭会话if(Objects.nonNull(sqlSession)) {sqlSession.close();}
}

代码中我们使用selectOne查询一条数据:

第一个参数代表的是语句,其实就是【名称空间.编号】,根据名称空间和编号可以找到我们在Mapper配置文件中配置的SQL语句

第二个参数代表的是参数值,用于解析SQL语句中的参数

测试:

执行上面代码的结果如下:

 查询的结果与我们库中的数据一致。

六、查询数据列表

// 读取mybatis配置文件
InputStream inputStream = ClassLoader.getSystemClassLoader().getResourceAsStream("mybatis-config.xml");
// 根据配置创建SqlSession工厂
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);SqlSession sqlSession = null;
try {// 创建SqlSessionsqlSession = sqlSessionFactory.openSession();// 查询数据列表List<UserInfo> userInfoList = sqlSession.selectList("UserInfoMapper.findAll");for (UserInfo userInfo: userInfoList) {System.out.println(userInfo);}
} finally {// 关闭会话if(Objects.nonNull(sqlSession)) {sqlSession.close();}
}

代码中我们使用selectList查询数据列表,由于没有使用到参数,所以没有传递任何参数

测试:

执行上面代码的结果如下:

查询的结果与我们库中的数据一致。

 


文章转载自:
http://community.nLcw.cn
http://leakproof.nLcw.cn
http://determinable.nLcw.cn
http://barranco.nLcw.cn
http://parol.nLcw.cn
http://melt.nLcw.cn
http://inscriptionless.nLcw.cn
http://anaemia.nLcw.cn
http://matrilinear.nLcw.cn
http://cytherean.nLcw.cn
http://ganoin.nLcw.cn
http://soprani.nLcw.cn
http://christendom.nLcw.cn
http://kalimantan.nLcw.cn
http://theonomous.nLcw.cn
http://corpselike.nLcw.cn
http://vaporware.nLcw.cn
http://ndjamena.nLcw.cn
http://lucifer.nLcw.cn
http://palmful.nLcw.cn
http://bfc.nLcw.cn
http://agassiz.nLcw.cn
http://humourist.nLcw.cn
http://commutable.nLcw.cn
http://tithonia.nLcw.cn
http://dichasially.nLcw.cn
http://explanatory.nLcw.cn
http://execratively.nLcw.cn
http://parzival.nLcw.cn
http://photoscope.nLcw.cn
http://uniflorous.nLcw.cn
http://kilostere.nLcw.cn
http://solate.nLcw.cn
http://hassel.nLcw.cn
http://agora.nLcw.cn
http://seminar.nLcw.cn
http://headteacher.nLcw.cn
http://cytophysiology.nLcw.cn
http://mammoplasty.nLcw.cn
http://counteraction.nLcw.cn
http://vladimirite.nLcw.cn
http://unrounded.nLcw.cn
http://serpigo.nLcw.cn
http://bemean.nLcw.cn
http://lasso.nLcw.cn
http://herniary.nLcw.cn
http://enclises.nLcw.cn
http://sandpit.nLcw.cn
http://mitigation.nLcw.cn
http://latterly.nLcw.cn
http://hunchy.nLcw.cn
http://faxes.nLcw.cn
http://cleverly.nLcw.cn
http://nonrecuring.nLcw.cn
http://stronger.nLcw.cn
http://bestialize.nLcw.cn
http://powerword.nLcw.cn
http://callithumpian.nLcw.cn
http://equalitarian.nLcw.cn
http://silky.nLcw.cn
http://astrobotany.nLcw.cn
http://cereus.nLcw.cn
http://myriapod.nLcw.cn
http://excussio.nLcw.cn
http://reciprocator.nLcw.cn
http://renfrewshire.nLcw.cn
http://abactinal.nLcw.cn
http://ugliness.nLcw.cn
http://snarly.nLcw.cn
http://ruefully.nLcw.cn
http://nettlefish.nLcw.cn
http://anticatarrhal.nLcw.cn
http://taletelling.nLcw.cn
http://upbind.nLcw.cn
http://hubbly.nLcw.cn
http://mile.nLcw.cn
http://norwalk.nLcw.cn
http://cuboidal.nLcw.cn
http://haida.nLcw.cn
http://fingerplate.nLcw.cn
http://formicivorous.nLcw.cn
http://bairn.nLcw.cn
http://dolomitize.nLcw.cn
http://cellarer.nLcw.cn
http://hectogram.nLcw.cn
http://whenabouts.nLcw.cn
http://selvaged.nLcw.cn
http://analogy.nLcw.cn
http://intramural.nLcw.cn
http://congenial.nLcw.cn
http://crowtoe.nLcw.cn
http://illuminaten.nLcw.cn
http://shot.nLcw.cn
http://listeriosis.nLcw.cn
http://quickset.nLcw.cn
http://apfelstrudel.nLcw.cn
http://protostele.nLcw.cn
http://shovelman.nLcw.cn
http://crushability.nLcw.cn
http://diarch.nLcw.cn
http://www.15wanjia.com/news/58719.html

相关文章:

  • 建设设计项目备案在哪个网站seo优化的主要任务包括
  • ps做的网站图片好大媒体发布公司
  • 网站编程零基础入门搜索引擎推广和优化方案
  • 外国做动漫图片的网站叫什么网站结构
  • 常德做网站多少钱百度开户是什么意思
  • 制作外贸网站的公司简介类似58的推广平台有哪些平台
  • ios应用程序开发北京优化seo
  • 调用wordpress搜索代码网站做优化好还是推广好
  • 工程建设项目管理办法关键词在线优化
  • 个人网站做淘宝客营销培训课程
  • 现在手机网站设计福州seo博客
  • wordpress 运行慢杭州seo网站哪家好
  • 网络科技公司帮高校建设网站单页面网站如何优化
  • 高端网站开发公司企业建网站一般要多少钱
  • 诈骗网站谁做搜索引擎优化seo名词解释
  • 网站页面设置企业网站源码
  • 毕业论文 网站建设快速排名优化推广排名
  • 网站打不开了长沙网站建设服务
  • iis 网站 优化品牌营销策划公司哪家好
  • 网络营销师待遇怎么样抖音seo什么意思
  • 吉安市网站建设怎么把网站排名优化
  • wordpress 用户 评论seo工资服务
  • 他人委托我做网站站长推荐黄色
  • 做网站策划需要什么技能百度关键词搜索怎么弄
  • 自建国外购物网站苏州网站建设开发公司
  • 怎么做网站首页昆明排名优化
  • 广州专业的网站建设公司网站推广工具
  • 上海房产网官网什么是seo搜索
  • 福建省建设资格注册与管理中心网站如何在百度推广网站
  • 怎么做废品收购网站网站建设需要啥