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

哪里找做网站的谷歌seo 优化

哪里找做网站的,谷歌seo 优化,怎么查询网站真假备案,微商平台怎么注册MyBatis框架的使用:mybatis介绍环境搭建基础sql的使用如何使用Map传入多个参数返回多个实体用List或者Map接收特殊sql的使用 一、MyBatis介绍1.1 特性1.2 下载地址1.3 和其它持久层技术对比 二、搭建环境2.1配置maven2.2 创建mybatis配置文件2.3 搭建测试环境 三、基…

MyBatis框架的使用:mybatis介绍+环境搭建+基础sql的使用+如何使用Map传入多个参数+返回多个实体用List或者Map接收+特殊sql的使用

    • 一、MyBatis介绍
      • 1.1 特性
      • 1.2 下载地址
      • 1.3 和其它持久层技术对比
    • 二、搭建环境
      • 2.1配置maven
      • 2.2 创建mybatis配置文件
      • 2.3 搭建测试环境
    • 三、基础的sql语句
      • 3.1 配置接口
      • 3.2 配置映射文件
      • 3.3 map集合类型的参数
    • 四、各种查询功能的实现
    • 五、特殊的sql语句

一、MyBatis介绍

1.1 特性

1) MyBatis 是支持定制化 SQL、存储过程以及高级映射的优秀的持久层框架

2) MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集

3) MyBatis可以使用简单的XML或注解用于配置和原始映射,将接口和Java的POJO(Plain Old Java Objects,普通的Java对象)映射成数据库中的记录

4) MyBatis 是一个半自动的ORM(Object Relation Mapping)框架

1.2 下载地址

在github上下载,在里面可以挑选我们需要的版本
https://github.com/mybatis/mybatis-3

1.3 和其它持久层技术对比

JDBC

  • SQL 夹杂在Java代码中耦合度高,导致硬编码内伤

  • 维护不易且实际开发需求中 SQL 有变化,频繁修改的情况多见

  • 代码冗长,开发效率低

Hibernate和JPA

  • 操作简便,开发效率高
  • 程序中的长难复杂 SQL 需要绕过框架
  • 内部自动生产的 SQL,不容易做特殊优化
  • 基于全映射的全自动框架,大量字段的 POJO 进行部分映射时比较困难。
  • 反射操作太多,导致数据库性能下降

Mybatis

  • 轻量级,性能出色
  • SQL 和 Java 编码分开,功能边界清晰。Java代码专注业务、SQL语句专注数据
  • 开发效率稍逊于Hibernate,但是完全能够接受

二、搭建环境

新建maven项目

2.1配置maven

在这里插入图片描述

加入pom依赖

<dependencies><!-- Mybatis核心 --><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>3.5.7</version></dependency><!-- junit测试 --><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version><scope>test</scope></dependency><!-- MySQL驱动 --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.6</version></dependency><!-- log4j日志 --><dependency><groupId>log4j</groupId><artifactId>log4j</artifactId><version>1.2.17</version></dependency></dependencies>

2.2 创建mybatis配置文件

习惯上命名为mybatis-config.xml,这个文件名仅仅只是建议,并非强制要求。将来整合Spring 之后,这个配置文件可以省略

核心配置文件主要用于配置连接数据库的环境以及MyBatis的全局配置信息

在这里插入图片描述

在标签中,使用引入属性文件 ,这里跟数据库的配置一一对应

    <properties resource="jdbc.properties"></properties>
  • < typeAliases > 标签设置类型别名,对于实体类可以直接使用类名,不区分大小写
  • < environments >配置数据库环境
  • < transactionManager >设置事务管理
  • < dataSource >设置数据源

这里数据库中的驱动jdbc.driver跟jdbc.properties文件中的value值对应

引入mybatis映射文件的方式有两种:

  • 直接引入,映射文件有多个的话需要一个一个的引入

  • 以包为单位引入,将包下的映射文件全部引入

    注意第二种方式:

    1.mapper接口所在的包要和映射文件所在的包一致

    2.mapper接口要和映射文件的名字一致

MySQL不同版本的注意事项

1、驱动类driver-class-name

MySQL 5版本使用jdbc5驱动,驱动类使用:com.mysql.jdbc.Driver

MySQL 8版本使用jdbc8驱动,驱动类使用:com.mysql.cj.jdbc.Driver

2、连接地址url

MySQL 5版本的url:

jdbc:mysql://localhost:3306/ssm

MySQL 8版本的url:

jdbc:mysql://localhost:3306/ssm?serverTimezone=UTC

<?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><properties resource="jdbc.properties"></properties>
<!--    设置类型的别名--><typeAliases>
<!--    typeAliases 设置类型别名 以包为单位,将包下所有的类型设置默认的类型别名,即类名,且不区分大小写 --><package name="com.entity"/></typeAliases><!--    数据库环境-->
<!--    environments配置多个连接数据库的环境default:设置默认使用的环境的idenvironment配置某个具体的环境属性:id 表示连接数据库环境的唯一标识,不能重复 --><environments default="development"><environment id="development"><transactionManager type="JDBC"/>
<!--          transactionManager:设置事务管理的方式属性 JDBC | MANAGEDJDBC:表示当前环境中,执行SQL时使用的是JDBC中原生的事务管理方式,事务的提交或回滚需要手动处理MANAGED:被管理,例如SpringdataSource:配置数据源type:设置数据源的类型type:POOLED | UNPOOLED | JNDIPOOLED:表示使用数据库连接池缓存数据库连接UNPOOLED:表示不使用数据库连接池缓存数据库连接JNDI:表示使用上下文中的数据源--><dataSource type="POOLED">
<!--                设置数据库的驱动--><property name="driver" value="${jdbc.driver}"/>
<!--                设置数据库的链接地址--><property name="url" value="${jdbc.url}"/>
<!--                设置连接数据库的用户名--><property name="username" value="root"/>
<!--                 设置连接数据库的密码--><property name="password" value="2020"/></dataSource></environment></environments>
<!--    引入mybatis映射文件-->
<!--    方式一-->
<!--    <mappers>-->
<!--        <mapper resource="mappers/UserMapper.xml"/>-->
<!--    </mappers>-->
<!--    方式二-->
<!--    以包为单位引入映射文件要求:mapper接口所在的包要和映射文件所在的包一致mapper接口要和映射文件的名字一致
--><mappers><package name="com.mapper"/></mappers>
</configuration>

创建属性文件,配置数据库连接的key-value值

在这里插入图片描述

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis_demo

接下来建好各级层次的包,尤其要注意com.mapper这里有两个,在resources下的目录是放置映射文件的,这两个包名需要对应

在这里插入图片描述

2.3 搭建测试环境

在utils下新建SqlSessionUtil类

package com.util;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;public class SqlSessionUtil {//获取核心配置文件的输入流private static InputStream inputStream = null;public static SqlSession getSqlSession(){SqlSession sqlSession = null;try {inputStream = Resources.getResourceAsStream("mybatis-config.xml");//获取SqlSessionFactoryBuilder对象SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();//获取SqlSessionFactory对象SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream);//获取SqlSession对象,不会自动提交事务//SqlSession sqlSession = sqlSessionFactory.openSession();//获取SqlSession对象,会自动提交事务sqlSession = sqlSessionFactory.openSession(true);} catch (IOException e) {e.printStackTrace();}return sqlSession;}public static void inputStreamClose(){try {inputStream.close();} catch (IOException e) {e.printStackTrace();}}
}

SqlSession:代表Java程序和数据库之间的会话(HttpSession是Java程序和浏览器之间的会话)

SqlSessionFactory:是“生产”SqlSession的“工厂”

工厂模式:如果创建某一个对象,使用的过程基本固定,就可以把创建这个对象的相关代码封装到一个“工厂类”中,以后都使用这个工厂类来“生产”所需要的对象。

然后在test.java下新建测试类SelectMapperTest,在run方法中编写需要测试的内容

public class SelectMapperTest {private SqlSession sqlSession = null;private SelectMapper mapper = null;@Beforepublic void init(){//获取sqlSession对象sqlSession = SqlSessionUtil.getSqlSession();//获取UserMapper的代理实现类对象mapper = sqlSession.getMapper(SelectMapper.class);}@Testpublic void run(){}@Afterpublic void destroy(){//提交事务//sqlSession.commit();//关闭资源sqlSession.close();SqlSessionUtil.inputStreamClose();}}

接下来可以在com.mapper中创建接口,在resource下的com.mapper中创建配置文件

在这里插入图片描述

public interface UserMapper {}
<?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.mapper.UserMapper"></mapper>

三、基础的sql语句

测试用的数据库信息

在这里插入图片描述

使用sql之前,在entity生成实体类User

3.1 配置接口

/**
* 插入
* @param user
* @return
*/
int insertUser(User user);
/**
* 删除
* @return
*/
void deleteUser();/*** 更新用户信息* @return*/
int updateUser();
/*** 查询用户信息*/
List<User> findUser();/*** 根据用户名查询用户信息*/
User getUserByUserName(String userName);
/*** 根据用户名和年龄查询用户信息*/
User gerUserByUserNameAndAge(@Param("userName") String userName,@Param("age") Integer age);

这里的注解@Param里面的内容与映射文件中的变量名对应

MyBatis获取参数值的两种方式:${}和#{}

  • ${}的本质就是字符串拼接,采用sql拼接,无法防止sql注入
  • #{}的本质就是占位符赋值 ,采用预编译 防止sql注入

如果mapper接口中的方法参数为单个的字面量类型,此时可以使用#{}和 以任意的名称获取参数的值,如果使用 {}以任意的名称获取参数的值,如果使用 以任意的名称获取参数的值,如果使用{},注意需要手动添加单引号

如果mapper接口中的方法参数为多个时,此时MyBatis会自动将这些参数放在一个map集合(key,value)中,以arg0, arg1…为键,以参数为值;或者以param1, param2…为键,以参数为值。因此只需要通过#{}和 访问 m a p 集合的键就可以获取相对于的值,如果使用 {}访问map集合的键就可以获取相对于的值,如果使用 访问map集合的键就可以获取相对于的值,如果使用{},注意需要手动添加单引号。

3.2 配置映射文件

查询的标签select必须设置属性resultType或resultMap,用于设置实体类和数据库表的映射 关系

resultType:自动映射,用于属性名和表中字段名一致的情况

resultMap:自定义映射,用于一对多或多对一或字段名和属性名不一致的情况

<insert id="insertUser">insert into user values(null ,#{userName},#{password},#{age},#{sex},#{email});
</insert><delete id="deleteUser">delete from user where id = 2;
</delete><update id="updateUser">update user set userName="李明" ,password="123456" where id = 3;
</update><select id="findUser" resultType="com.entity.User">select * from user;
</select><select id="getUserByUserName" resultType="user">select * from user where userName='${userName}';
</select><select id="gerUserByUserNameAndAge" resultType="user">select  * from user where userName='${userName}' and age = '${age}';
</select>

3.3 map集合类型的参数

如果mapper接口中的方法参数为多个时,此时可以手动创建一个map集合,将这些参数放在map集合中,只需要通过#{}和 访问 m a p 集合的键(自己定义的键)就可以获取相对于的值,如果使用 {}访问map集合的键(自己定义的键)就可以获取相对于的值,如果使用 访问map集合的键(自己定义的键)就可以获取相对于的值,如果使用{},注意需要手动添加单引号。

接口,byMap方法传入一个Map

User byMap(Map<String , Object> map);

映射文件,userName是用户名,age是年龄,大括号里面的是自定义的,传入的map也应该使用这两个变量名

<select id="byMap" resultType="user">select  * from user where userName='${userName}' and age = '${age}';
</select>

测试类,查询指定姓名和年龄的用户

Map<String,Object> map=new HashMap<>();map.put("age",18);
map.put("userName","李明");
System.out.println(mapper.byMap(map));

四、各种查询功能的实现

配置接口

public interface SelectMapper {/*** 根据id查询用户信息** @param id* @return*/User getUserById(@Param("id") Integer id);/*** 查询所有用户信息,不确定有多少用户可以用集合* @return*/List<User> getAllUser();/**** 获取数量* @return*/Integer getCount();/*** 查询单个用户信息,返回一个map* @param id* @return*/Map<String,Object> getUserByIdToMap(Integer id);/*** 方式一:* 查询所有,结果返回一个list* @return*/List<Map<String,Object>> getAllUserToList();/*** 方式二:* 加入注解,返回一个  id:用户信息  的map* @return*/@MapKey("id")Map<String,Object> getAllUserToMap();}

配置映射文件

<select id="getUserById" resultType="user">select * from user where id = #{id};
</select><select id="getAllUser" resultType="user">select * from user;
</select><select id="getCount" resultType="java.lang.Integer">select count(*) from user;
</select><select id="getUserByIdToMap" resultType="map">select * from user where id = #{id};
</select><select id="getAllUserToList" resultType="map">select * from user;
</select><select id="getAllUserToMap" resultType="map">select * from user;
</select>

getUserByIdToMap查询单个用户信息,返回一个map

Map<String,Object> map = mapper.getUserByIdToMap(3);
System.out.println(map);

在这里插入图片描述

getAllUserToList查询所有,结果返回一个list

System.out.println(mapper.getAllUserToList());

在这里插入图片描述

getAllUserToMap加入注解,返回一个 id:用户信息 的map

System.out.println(mapper.getAllUserToMap());

在这里插入图片描述

五、特殊的sql语句

/*** 模糊查询* @param temp* @return*/
List<User> getUserLike(@Param("temp") String temp);/*** 批量删除* @param ids*/
void deleteMoreUser(@Param("ids") String ids);
    <select id="getUserLike" resultType="user">
<!--1. select * from user where userName like concat('%',#{temp},'%')  会使可移植性变差2.select * from user where userName like ”%“#{temp}“%”3.select * from user where userName like '%${temp}%';-->select * from user where userName like '%${temp}%';</select><delete id="deleteMoreUser" ><--这里注意使用${}的方式,且不能加单引号-->delete from user where id in(${ids});</delete>

模糊查询中字符串的拼接有三种方式,temp是传入的值

  • select * from user where userName like concat(‘%’,#{temp},‘%’)
  • select * from user where userName like ”%“#{temp}“%”
  • select * from user where userName like ‘%${temp}%’
System.out.println(mapper.getUserLike("lm"));

在这里插入图片描述

mapper.deleteMoreUser("10,11");

这里的10,11是需要删除信息的id范围


文章转载自:
http://patrist.c7630.cn
http://honorand.c7630.cn
http://ambatch.c7630.cn
http://serpent.c7630.cn
http://tharm.c7630.cn
http://revival.c7630.cn
http://homosexuality.c7630.cn
http://tricky.c7630.cn
http://twae.c7630.cn
http://jv.c7630.cn
http://muscadine.c7630.cn
http://latecomer.c7630.cn
http://sternum.c7630.cn
http://chlorobenzene.c7630.cn
http://cystamine.c7630.cn
http://researchful.c7630.cn
http://thread.c7630.cn
http://decidua.c7630.cn
http://verligte.c7630.cn
http://mulish.c7630.cn
http://intal.c7630.cn
http://chirimoya.c7630.cn
http://fallibility.c7630.cn
http://inhabitancy.c7630.cn
http://defoaming.c7630.cn
http://scourings.c7630.cn
http://ira.c7630.cn
http://blindstory.c7630.cn
http://eloquence.c7630.cn
http://danish.c7630.cn
http://ixodid.c7630.cn
http://xography.c7630.cn
http://hydrogasification.c7630.cn
http://unpopular.c7630.cn
http://brahma.c7630.cn
http://cruelly.c7630.cn
http://rhetorician.c7630.cn
http://calumny.c7630.cn
http://amortisation.c7630.cn
http://fresco.c7630.cn
http://spermophyte.c7630.cn
http://neurotomy.c7630.cn
http://phenylbutazone.c7630.cn
http://militant.c7630.cn
http://hypo.c7630.cn
http://abhenry.c7630.cn
http://enmity.c7630.cn
http://quayside.c7630.cn
http://illuminance.c7630.cn
http://nlrb.c7630.cn
http://washstand.c7630.cn
http://slapjack.c7630.cn
http://inhabitativeness.c7630.cn
http://contumelious.c7630.cn
http://renegotiation.c7630.cn
http://hypotonic.c7630.cn
http://heterometabolous.c7630.cn
http://trouble.c7630.cn
http://guideline.c7630.cn
http://scoliosis.c7630.cn
http://codlinsandcream.c7630.cn
http://intilted.c7630.cn
http://outturn.c7630.cn
http://callboy.c7630.cn
http://mound.c7630.cn
http://rainwater.c7630.cn
http://temple.c7630.cn
http://vasectomize.c7630.cn
http://horny.c7630.cn
http://bouffe.c7630.cn
http://oeo.c7630.cn
http://embroidery.c7630.cn
http://flite.c7630.cn
http://shiny.c7630.cn
http://trollop.c7630.cn
http://biophysics.c7630.cn
http://uncaused.c7630.cn
http://horseflesh.c7630.cn
http://bogota.c7630.cn
http://vulcanizate.c7630.cn
http://dihydric.c7630.cn
http://niggertoe.c7630.cn
http://dayworker.c7630.cn
http://tungsten.c7630.cn
http://flitch.c7630.cn
http://solace.c7630.cn
http://feverishly.c7630.cn
http://access.c7630.cn
http://palatine.c7630.cn
http://adrenodoxin.c7630.cn
http://costumey.c7630.cn
http://acis.c7630.cn
http://metal.c7630.cn
http://harvestry.c7630.cn
http://fot.c7630.cn
http://clay.c7630.cn
http://almond.c7630.cn
http://apocalypticist.c7630.cn
http://semibarbarian.c7630.cn
http://dps.c7630.cn
http://www.zhongyajixie.com/news/91632.html

相关文章:

  • 一个网站域名一年要多少钱成都最新动态
  • 临沂做网站的公司站长工具高清无吗
  • 2017优惠券网站怎么做怎么接推广
  • 做网站软件要钱吗百度seo关键词工具
  • 网站建设推广优化岗位说明书淘宝推广哪种方式最好
  • 设计师网课北京seo公司司
  • 网站建设实验总结报告站长工具免费
  • 双通网络网站建设b2b平台推广网站
  • 全免费无代码开发平台上海百度搜索优化
  • 德州北京网站建设理发培训专业学校
  • 金华网站建设方案开发seo搜索引擎优化就业指导
  • 做淘宝客网站哪个好用郑州百度网站快速优化
  • 相亲网站认识的可以做朋友学网络营销去哪个学校
  • 有创意的设计公司名字大全seo自学教程
  • 建设网站域名备案查询百度高级搜索技巧
  • 企业大型网站开发设计建站流程推广的公司
  • 哪个网站做二微码指数分布
  • 二级域名怎么设置seo推广培训
  • 公司域名怎么取比较好seo赚钱吗
  • 装修队做网站关键词排名怎样
  • 专门做茶叶的网站seo网站优化培训怎么做
  • 怎么做网站宣传爱站网挖掘词
  • 深圳分销网站设计电话独立站seo怎么做
  • 做网站大概要多去哪里推广软件效果好
  • wordpress的伪静态seo站内优化最主要的是什么
  • 网站建设好学吗seo专员是什么意思
  • wordpress滑动登录深圳网站营销seo电话
  • wordpress技术服务信息流广告优化
  • 网站用什么做备份网络广告投放平台
  • 政府网站建设北京市第一品牌营销策略包括哪些内容