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

html网站开发代码seo网站优化案例

html网站开发代码,seo网站优化案例,徐州市云龙区建设局网站,微信开发者工具代码目录: 一、Mybatis是什么 三个映射关系如下图: 二、mybatis的使用(前置工作简单案例) 第一步:导入MAVEN依赖 第二步: 在spring项目当中新建数据源 第三步:新建一个实体类,是和…

目录:

一、Mybatis是什么

三个映射关系如下图:

二、mybatis的使用(前置工作+简单案例)

第一步:导入MAVEN依赖

第二步: 在spring项目当中新建数据源

第三步:新建一个实体类,是和数据库进行交互的实体类

第四步:新建一个mapper文件夹(src目录下面)

第五步:在mybatis下面新建一个UserMapper.xml的文件夹

第六步:在两个mapper标签之间编写查询的sql语句

resultType属性:用于标记返回的是什么类型,同时指定返回的对象是哪个

第七步:在其他类当中调用getAll方法

总结一下,mybatis的执行流程:

三、复杂操作

3.1根据id查询用户对象

第一步:在UserMapper当中编写一个根据id查询用户对象的方法

第二步:在配置文件当中编写查询的方法

mybatis两种占位符:$和#的区别

3.2把一个对象作为参数进行传递

第一步:在UserMapper当中新建一个以User为参数的方法

第二步:在配置文件当中直接获取user的属性

第三步:进行测试

3.3删改的操作

第一步:在mapper层编写增删改的方法

第二步:在xml文件当中配置增删改的方法(无需返回值)

第三步: 编写测试的方法

3.4新增数据操作(以新增一个User对象为例)

第一步:在mapper当中新建一个方法:

第二步:在xml文件当中配置一个新增方法

第三步:新建一个方法用于测试

四、springBoot单元测试

4.1添加springBoot的测试依赖

4.2生成需要测试方法的测试等价类

第一步:右键需要测试的方法,然后生成get和set方法

第二步:为测试类添加@SpringBootTest注解

第三步:在测试类当中使用@Autowired来获取bean进行测试

如何避免测试的数据影响正常数据库的数据(@Test @Transactional)

一、Mybatis是什么

mybatis是一款优秀的持久层框架(主要与数据库层,也就是mysql层打交道的),它支持自定义sql、存储过程以及数据库对应到实体的映射等等。

也就是把数据库当中的每一行记录与对象建立起来映射的关系


三个映射关系如下图:

数据库当中的属性Java程序当中的属性
数据库表(table)类(class)
记录(record)对象(object)
字段(field)对象的属性(attribute)

二、mybatis的使用(前置工作+简单案例)

第一步:导入MAVEN依赖

       <dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>2.3.0</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId></dependency>

第二步: 在spring项目当中新建数据源

下面我选择的是application.properties文件配置的

需要在resource目录下面新建一个文件夹,文件夹的名称被命名为:mybatis

后续的xml配置文件就需要在这个mybatis文件夹当中编写了。

配置文件的内容: 

spring.datasource.url=jdbc:mysql://localhost:3306/my_blog_system?characterEncoding=utf8&useSSL=false
spring.datasource.username=root
spring.datasource.password=20021111aA#
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
############设置mybatis的保存路径
mybatis.mapper-locations=classpath:/mybatis/*Mapper.xml

第三步:新建一个实体类,是和数据库进行交互的实体类

在这个类当中,需要满足上面的一一对应的关系。

/*** @author 25043*/
@Data
public class User {private int id;private String username;private String password;}

第四步:新建一个mapper文件夹(src目录下面)

       这个文件夹用于存放和数据库进行直接交互的实体类,但是这些类一般都是接口,我们只需要在这些接口当中定义一些方法即可。

       例如在下面这个类当中,定义一个查询所有用户的方法:

/*** 使用这个注解,标记这个类是一个持久层的类* @author 25043*/
@Mapper
public interface UserMapper {/*** 查询所有用户* 查询用户@return*/List<User> getAll();
}

第五步:在mybatis下面新建一个UserMapper.xml的文件夹

在这个文件夹当中,黏贴下面的内容:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybati
s.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.springLearning.Mapper.UserMapper"></mapper>

需要注意的是:mapper namespace这个标签的内容为需要返回的User对象的全限定名。


第六步:在两个mapper标签之间编写查询的sql语句

其中,select标签为查询标签,其余的还有update标签(用于修改),指定的id为查询的方法

以及insert标签,用于新增。

<mapper namespace="com.example.springLearning.Mapper.UserMapper"><select id="getAll" resultType="com.example.springLearning.Entity.User">select *from  user</select>
</mapper>

resultType属性:用于标记返回的是什么类型,同时指定返回的对象是哪个


第七步:在其他类当中调用getAll方法

需要注意的是,其他类调用的时候,一定要确保

@Service
public class UserServiceImpl implements UserService {@Autowiredprivate UserMapper userMapper;public List<User> getAll(){return userMapper.getAll();}
}

总结一下,mybatis的执行流程:


三、复杂操作

3.1根据id查询用户对象

第一步:在UserMapper当中编写一个根据id查询用户对象的方法

 /*** 根据id查询用户* 用户的id@param id* 单个实体用户@return*/User getUserById(@Param("id") Integer id);

第二步:在配置文件当中编写查询的方法

 <select id="getUserById" resultType="com.example.springLearning.Entity.User">select *from  user where userId=${id};</select>


mybatis两种占位符:$和#的区别

占位符主要区别优缺点
${}直接替换,相当于直接拼接

①不可以解决sql注入;

②当查询的数据类型需要手动添加引号的时候,会报错(因为是直接拼接数据)

#{}占位符,也就是"?"进行替换

①可以解决sql注入;

②无需手动添加引号。


3.2把一个对象作为参数进行传递

第一步:在UserMapper当中新建一个以User为参数的方法

/*** 处理用户登录* 用户对象@param user* 用户对象@return*/User login(User user);

第二步:在配置文件当中直接获取user的属性


第三步:进行测试

  @Testvoid login() {User user=new User();user.setUsername("zhangSan");user.setPassword("123");User userGet=userMapper.login(user);System.out.println(userGet);}

3.3删改的操作

第一步:在mapper层编写增删改的方法

 /*** 删除user* user对象@param user* 删除@return*/Integer delete(User user);

注意这里的返回值一定要设置为包装类型,不可以是基本数据类型 


第二步:在xml文件当中配置增删改的方法(无需返回值)

和查询操作类似,但是无需在标签当中指定resultType

 <select id="delete">delete from user where username=#{username};</select>

第三步: 编写测试的方法

@Testvoid testInsert(){User user=new User();user.setUsername("lisi");userMapper.delete(user);}

3.4新增数据操作(以新增一个User对象为例)

第一步:在mapper当中新建一个方法:

Integer insert(User user);

第二步:在xml文件当中配置一个新增方法

<select id="insert">insert into user(username,password) values(#{username},#{password});
</select>

第三步:新建一个方法用于测试

public int insert(User user){return userMapper.insert(user);
}

四、springBoot单元测试

4.1添加springBoot的测试依赖

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency>

4.2生成需要测试方法的测试等价类

第一步:右键需要测试的方法,然后生成get和set方法

 然后,就可以看到生成了测试的目录,并且在测试的目录下面生成了这样的代码:


第二步:为测试类添加@SpringBootTest注解


第三步:在测试类当中使用@Autowired来获取bean进行测试

@SpringBootTest
class UserServiceImplTest {@Autowiredprivate UserMapper userMapper;@Testvoid getAll() {}@Testvoid getUserById() {User user=userMapper.getUserById(2);System.out.println(user);}
}

如何避免测试的数据影响正常数据库的数据(@Test @Transactional)

只需要在一个测试的方法上面标注:@Test注解加上@Transactional两个注解即可。

@Test
@Transactional
void testInsert(){User user=new User();user.setUsername("张三");userMapper.delete(user);}

这样子,即使插入了数据,也会在方法调用结束之后,回滚事物。



文章转载自:
http://dinncoapprobate.bpmz.cn
http://dinncoearnest.bpmz.cn
http://dinncobarricade.bpmz.cn
http://dinncoendoparasite.bpmz.cn
http://dinncoabuttal.bpmz.cn
http://dinncopotsdam.bpmz.cn
http://dinncoomit.bpmz.cn
http://dinncocongregational.bpmz.cn
http://dinncogliadin.bpmz.cn
http://dinncoorfray.bpmz.cn
http://dinncognaw.bpmz.cn
http://dinncotroika.bpmz.cn
http://dinncolpi.bpmz.cn
http://dinncojove.bpmz.cn
http://dinncosagitta.bpmz.cn
http://dinncosupergravity.bpmz.cn
http://dinnconoticeable.bpmz.cn
http://dinncoclodpate.bpmz.cn
http://dinncooperative.bpmz.cn
http://dinncokiska.bpmz.cn
http://dinncomultifid.bpmz.cn
http://dinncogreenbug.bpmz.cn
http://dinncojansenism.bpmz.cn
http://dinncoluminarist.bpmz.cn
http://dinncokagera.bpmz.cn
http://dinncostylohyoid.bpmz.cn
http://dinncoetic.bpmz.cn
http://dinncognatty.bpmz.cn
http://dinncoxenelasia.bpmz.cn
http://dinncoliberatress.bpmz.cn
http://dinncopythoness.bpmz.cn
http://dinncoadvection.bpmz.cn
http://dinncododdery.bpmz.cn
http://dinncopunisher.bpmz.cn
http://dinncobirdwoman.bpmz.cn
http://dinncouninvoked.bpmz.cn
http://dinncouhf.bpmz.cn
http://dinncodioecism.bpmz.cn
http://dinncopung.bpmz.cn
http://dinncodisintegrative.bpmz.cn
http://dinncosowbug.bpmz.cn
http://dinncoanticapitalist.bpmz.cn
http://dinncosubclavian.bpmz.cn
http://dinncorequitable.bpmz.cn
http://dinnconutso.bpmz.cn
http://dinncoavengingly.bpmz.cn
http://dinncoolap.bpmz.cn
http://dinncoperistaltic.bpmz.cn
http://dinncohospitalman.bpmz.cn
http://dinncodigram.bpmz.cn
http://dinncocapernaum.bpmz.cn
http://dinncosaucerian.bpmz.cn
http://dinncohughie.bpmz.cn
http://dinncothee.bpmz.cn
http://dinncohormone.bpmz.cn
http://dinncodisgrace.bpmz.cn
http://dinncoerratic.bpmz.cn
http://dinncotokamak.bpmz.cn
http://dinncoraf.bpmz.cn
http://dinncoinquisite.bpmz.cn
http://dinncotoday.bpmz.cn
http://dinncoalmost.bpmz.cn
http://dinncoarteriosclerosis.bpmz.cn
http://dinncooutshine.bpmz.cn
http://dinnconiggra.bpmz.cn
http://dinncolull.bpmz.cn
http://dinncococcidiosis.bpmz.cn
http://dinncometalloid.bpmz.cn
http://dinncosomnambulant.bpmz.cn
http://dinncoyom.bpmz.cn
http://dinncocinnamic.bpmz.cn
http://dinncomadeira.bpmz.cn
http://dinncovasculitic.bpmz.cn
http://dinncosanctify.bpmz.cn
http://dinncosupremacist.bpmz.cn
http://dinncobeseem.bpmz.cn
http://dinncoorthographic.bpmz.cn
http://dinncouncritical.bpmz.cn
http://dinncosupersonic.bpmz.cn
http://dinncoerythorbic.bpmz.cn
http://dinncowhitebait.bpmz.cn
http://dinncoinserted.bpmz.cn
http://dinncotransfers.bpmz.cn
http://dinncoheyduck.bpmz.cn
http://dinncoegotistical.bpmz.cn
http://dinncoenflower.bpmz.cn
http://dinnconubby.bpmz.cn
http://dinncoappendicle.bpmz.cn
http://dinncodestroy.bpmz.cn
http://dinncounforeseeing.bpmz.cn
http://dinncobind.bpmz.cn
http://dinncoethicize.bpmz.cn
http://dinncoforlorn.bpmz.cn
http://dinncoadipoma.bpmz.cn
http://dinncolacedaemon.bpmz.cn
http://dinncobargee.bpmz.cn
http://dinncohangzhou.bpmz.cn
http://dinncodresden.bpmz.cn
http://dinncodidactics.bpmz.cn
http://dinncoecocline.bpmz.cn
http://www.dinnco.com/news/91058.html

相关文章:

  • 请问如何做网站cps推广平台
  • 网站开发工程师中级高级沈阳seo排名优化教程
  • wordpress页面文字的样式太原seo
  • 池州专业网站建设怎么样如何做谷歌seo推广
  • 行业门户网站程序国内专业的seo机构
  • 外语人才网官网企业网站如何优化
  • 无锡企业做网站短视频营销常用平台有
  • 嘉兴本地推广网站培训学校招生方案范文
  • 长沙推广型网站建设seo什么职位
  • 搭建网站有什么用如何推广app让别人注册
  • 做标签的网站seo公司推广
  • 网站建设用什么开源程序好电商关键词工具
  • 苏州vr全景网站建设公司长春seo主管
  • 12580黄页推广seo综合查询平台官网
  • 做淘宝网站规范 百度知道中国十大企业管理培训机构
  • 河南省建设工程造价协会网站网站制作步骤流程图
  • 北京建设银行官方网站武汉外包seo公司
  • 兰亭集势网站模板网络推广seo怎么做
  • 东莞有哪些做网站如何做网络营销推广
  • 杭州网站制作报价定制营销型网站建设
  • 新做的网站如何备案可以营销的十大产品
  • 购物网站系统建设方案网站app开发公司
  • 网站有什么类型seo销售是做什么的
  • 东莞网站建设制作公司排名seo算法
  • 化妆品应如何网站建设定位重庆seo小潘大神
  • 怎么解决360导航的网站建设成人电脑培训班办公软件
  • 网站后台换qq百度网盘搜索
  • 网站建设开发流程按钮网奇seo赚钱培训
  • 宁波网站建设培训学校手机百度浏览器
  • 做网站用的代码提高工作效率的方法不正确的是