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

一个新的网站怎么做SEO优化时尚网站模板代码

一个新的网站怎么做SEO优化,时尚网站模板代码,wordpress批量删除函数,wordpress中文表单生成文章目录 映射关系多对一映射关系-官方文档映射关系多对1-基本介绍基本介绍注意细节 映射关系多对1-映射方式映射方式配置Mapper.xml 方式-应用实例注解实现多对1 映射-应用实例 映射关系多对一 映射关系-官方文档 文档地址: https://mybatis.org/mybatis-3/zh/sqlmap-xml.ht… 文章目录 映射关系多对一映射关系-官方文档映射关系多对1-基本介绍基本介绍注意细节 映射关系多对1-映射方式映射方式配置Mapper.xml 方式-应用实例注解实现多对1 映射-应用实例 映射关系多对一 映射关系-官方文档 文档地址: https://mybatis.org/mybatis-3/zh/sqlmap-xml.html 映射关系多对1-基本介绍 基本介绍 项目中多对1 的关系是一个基本的映射关系, 多对1, 也可以理解成是1 对多.User — Pet 一个用户可以养多只宠物Dep —Emp : 一个部门可以有多个员工 注意细节 我们直接讲双向的多对一的关系单向的多对一比双向的多对一简单。在实际的项目开发中, 要求会使用双向的多对一的映射关系什么是双向的多对一的关系: 比如通过User 可以查询到对应的Pet, 反过来通过Pet 也可以级联查询到对应的User 信息.多对多的关系是在多对1 的基础上扩展. 映射关系多对1-映射方式 映射方式 方式1通过配置XxxMapper.xml 实现多对1 方式2通过注解的方式实现多对1 配置Mapper.xml 方式-应用实例 需求说明: 实现级联查询,通过user 的id 可以查询到用户信息并可以查询到关联的pet信息 ​ 反过来通过Pet 的id 可以查询到Pet 的信息并且可以级联查询到它的主人User对象信息。 创建mybatis_user 和mybatis_pet 表 CREATE TABLE mybatis_user (id INT PRIMARY KEY AUTO_INCREMENT,NAME VARCHAR(32) NOT NULL DEFAULT )CHARSETutf8 ;CREATE TABLE mybatis_pet (id INT PRIMARY KEY AUTO_INCREMENT,nickname VARCHAR(32) NOT NULL DEFAULT ,user_id INT ,FOREIGN KEY (user_id) REFERENCES mybatis_user(id) )CHARSETutf8 ;INSERT INTO mybatis_user VALUES(NULL,宋江),(NULL,张飞); INSERT INTO mybatis_pet VALUES(1,黑背,1),(2,小哈,1); INSERT INTO mybatis_pet VALUES(3,波斯猫,2),(4,贵妃猫,2);SELECT * FROM mybatis_user; SELECT * FROM mybatis_pet;创建src\main\java\com\nlc\entity\Pet.java public class Pet {private Integer id;private String nickname;//一个pet对应一个主人 User对象private User user;public Integer getId() {return id;}public void setId(Integer id) {this.id id;}public String getNickname() {return nickname;}public void setNickname(String nickname) {this.nickname nickname;}public User getUser() {return user;}public void setUser(User user) {this.user user;}} 创建src\main\java\com\nlc\entity\User.java public class User {private Integer id;private String name;//因为一个user可以养多个宠物,mybatis 使用集合ListPet体现这个关系private ListPet pets;public Integer getId() {return id;}public void setId(Integer id) {this.id id;}public String getName() {return name;}public void setName(String name) {this.name name;}public ListPet getPets() {return pets;}public void setPets(ListPet pets) {this.pets pets;}//这toString会带来麻烦会造成StackOverFlow//Override//public String toString() {// return User{ // id id // , name name \ // , pets pets // };//} }创建PetMapper.java public interface PetMapper {//通过User的id来获取pet对象可能有多个因此使用List接收public ListPet getPetByUserId(Integer userId);//通过pet的id获取Pet对象, 同时会查询到pet对象关联的user对象public Pet getPetById(Integer id); }创建UserMapper.java public interface UserMapper {//通过id获取User对象public User getUserById(Integer id);}创建UserMapper.xml ?xml version1.0 encodingUTF-8 ? !DOCTYPE mapperPUBLIC -//mybatis.org//DTD Mapper 3.0//ENhttp://mybatis.org/dtd/mybatis-3-mapper.dtd mapper nameNlcacecom.nlc.mapper.UserMapper!--解读1、一定要想一想我们前面1-1是如何实现2、配置/实现 public User getUserById(Integer id);3、思路(1) 先通过user-id 查询得到user信息 (2) 再根据user-id查询对应的pet信息并映射到User-ListPet pets--resultMap idUserResultMap typeUserid propertyid columnid/result propertyname columnname/!--解读因为pets属性是集合因此这里需要是collection标签来处理1. ofTypePet 指定返回的集合中存放的数据类型Pet2. collection 表示 pets 是一个集合3. propertypets 是返回的user对象的属性 pets4. columnid SELECT * FROM mybatis_user WHERE id #{id} 返回的id字段对应的值--collection propertypets columnid ofTypePetselectcom.nlc.mapper.PetMapper.getPetByUserId//resultMapselect idgetUserById parameterTypeInteger resultMapUserResultMapSELECT * FROM mybatis_user WHERE id #{id}/select/mapper创建PetMapper.xml ?xml version1.0 encodingUTF-8 ? !DOCTYPE mapperPUBLIC -//mybatis.org//DTD Mapper 3.0//ENhttp://mybatis.org/dtd/mybatis-3-mapper.dtd mapper nameNlcacecom.nlc.mapper.PetMapper!--1、通过User的id来获取pet对象可能有多个因此使用List接收2、public ListPet getPetByUserId(Integer userId);3. 完成的思路和前面大体相同.--resultMap idPetResultMap typePetid propertyid columnid/result propertynickname columnnickname/association propertyuser columnuser_idselectcom.nlc.mapper.UserMapper.getUserById //resultMapselect idgetPetByUserId parameterTypeInteger resultMapPetResultMapSELECT * FROM mybatis_pet WHERE user_id #{userId}/select!--说明1. 注意体会resultMap带来好处 直接复用2. 实现/配置public Pet getPetById(Integer id);3. 通过pet的id获取Pet对象--select idgetPetById parameterTypeInteger resultMapPetResultMapSELECT * FROM mybatis_pet WHERE id #{id}/select /mapper创建PetMapperTest.java 完成测试 public class PetMapperTest {//属性private SqlSession sqlSession;private PetMapper petMapper;//初始化Beforepublic void init() {//获取到sqlSessionsqlSession MyBatisUtils.getSqlSession();petMapper sqlSession.getMapper(PetMapper.class);}Testpublic void getPetByUserId() {ListPet pets petMapper.getPetByUserId(2);for (Pet pet : pets) {System.out.println(pet信息- pet.getId() - pet.getNickname());User user pet.getUser();System.out.println(user信息 name- user.getName());}if(sqlSession ! null) {sqlSession.close();}}Testpublic void getPetById() {Pet pet petMapper.getPetById(2);System.out.println(pet信息- pet.getId() - pet.getNickname());User user pet.getUser();System.out.println(user信息- user.getId() - user.getName());if(sqlSession ! null) {sqlSession.close();}} }创建UserMapperTest.java 完成测试 public class UserMapperTest {//属性private SqlSession sqlSession;private UserMapper userMapper;//初始化Beforepublic void init() {//获取到sqlSessionsqlSession MyBatisUtils.getSqlSession();userMapper sqlSession.getMapper(UserMapper.class);}Testpublic void getUserById() {User user userMapper.getUserById(2);System.out.println(user信息- user.getId() - user.getName());ListPet pets user.getPets();for (Pet pet : pets) {System.out.println(养的宠物信息- pet.getId() - pet.getNickname());}if(sqlSession ! null) {sqlSession.close();}} }注解实现多对1 映射-应用实例 需求说明: 通过注解的方式来实现下面的多对1 的映射关系实现级联查询, 完成前面完成的任务通过User–Pet 也可Pet-User 在实际开发中推荐使用配置方式来做 创建UserMapperAnnotation.java // UserMapperAnnotation以注解的方式来配置多对一 public interface UserMapperAnnotation {//通过id获取User对象/*** 1. 注解的配置就是对应的Mapper.xml文件配置的改写* 2.* 1、一定要想一想我们前面1-1是如何实现* 2、配置/实现 public User getUserById(Integer id);* 3、思路(1) 先通过user-id 查询得到user信息 (2) 再根据user-id查询对应的pet信息* 并映射到User-ListPet pets* resultMap idUserResultMap typeUser* id propertyid columnid/* result propertyname columnname/* 1. ofTypePet 指定返回的集合中存放的数据类型Pet* 2. collection 表示 pets 是一个集合* 3. propertypets 是返回的user对象的属性 pets* 4. columnid SELECT * FROM mybatis_user WHERE id #{id} 返回的id字段对应的值* collection propertypets columnid ofTypePet* selectcom.nlc.mapper.PetMapper.getPetByUserId/* /resultMap* select idgetUserById parameterTypeInteger resultMapUserResultMap* SELECT * FROM mybatis_user WHERE id #{id}* /select*/Select(SELECT * FROM mybatis_user WHERE id #{id})Results({Result(id true, property id, column id),Result(property name, column name),//这里注意pets属性对应的是集合Result(property pets,column id,many Many(select com.nlc.mapper.PetMapperAnnotation.getPetByUserId))})public User getUserById(Integer id); }创建PetMapperAnnotation.java public interface PetMapperAnnotation {//通过User的id来获取pet对象可能有多个因此使用List接收/*** 1、通过User的id来获取pet对象可能有多个因此使用List接收* 2、public ListPet getPetByUserId(Integer userId);* 3. 完成的思路和前面大体相同.* resultMap idPetResultMap typePet* id propertyid columnid/* result propertynickname columnnickname/* association propertyuser columnuser_id* selectcom.nlc.mapper.UserMapper.getUserById /* /resultMap* select idgetPetByUserId parameterTypeInteger resultMapPetResultMap* SELECT * FROM mybatis_pet WHERE user_id #{userId}* /select*///id PetResultMap 就是给我们的Results[Result Map] 指定一个名字//目的是为了后面复用Select(SELECT * FROM mybatis_pet WHERE user_id #{userId})Results(id PetResultMap, value {Result(id true, property id, column id),Result(property nickname, column nickname),Result(property user,column user_id,one One(select com.nlc.mapper.UserMapperAnnotation.getUserById))})public ListPet getPetByUserId(Integer userId);//通过pet的id获取Pet对象, 同时会查询到pet对象关联的user对象/*** select idgetPetById parameterTypeInteger resultMapPetResultMap* SELECT * FROM mybatis_pet WHERE id #{id}* /select* ResultMap(PetResultMap) 使用/引用我们上面定义的 Results[ResultMap]*/Select(SELECT * FROM mybatis_pet WHERE id #{id})ResultMap(PetResultMap)public Pet getPetById(Integer id); } 创建UserMapperAnnotationTest.java 完成测试 public class UserMapperAnnotationTest {//属性private SqlSession sqlSession;private UserMapperAnnotation userMapperAnnotation;//初始化Beforepublic void init() {//获取到sqlSessionsqlSession MyBatisUtils.getSqlSession();userMapperAnnotation sqlSession.getMapper(UserMapperAnnotation.class);}Testpublic void getUserById() {User user userMapperAnnotation.getUserById(2);System.out.println(user信息- user.getId() - user.getName());ListPet pets user.getPets();for (Pet pet : pets) {System.out.println(宠物信息- pet.getId() - pet.getNickname());}if(sqlSession ! null) {sqlSession.close();}} }创建PetMapperAnnotationTest.java 完成测试 public class PetMapperAnnotationTest {//属性private SqlSession sqlSession;private PetMapperAnnotation petMapperAnnotation;//初始化Beforepublic void init() {//获取到sqlSessionsqlSession MyBatisUtils.getSqlSession();petMapperAnnotation sqlSession.getMapper(PetMapperAnnotation.class);}Testpublic void getPetByUserId() {ListPet pets petMapperAnnotation.getPetByUserId(1);for (Pet pet : pets) {System.out.println(宠物信息- pet.getId() - pet.getNickname());}if(sqlSession ! null) {sqlSession.close();}}Testpublic void getPetById() {Pet pet petMapperAnnotation.getPetById(1);System.out.println(pet信息- pet.getId() - pet.getNickname());User user pet.getUser();System.out.println(user信息- user.getId() - user.getName());if(sqlSession ! null) {sqlSession.close();}} }
http://www.hkea.cn/news/14426215/

相关文章:

  • 网站建设分金手指排名十一seo 费用
  • 网站推广怎么做优化打造一个app需要多少钱
  • 网站管理模板浦口区网站建设经验丰富
  • 域名网站备案管理系统连云港网站 建设
  • 湖南省建设厅政务中心网站汽车网站开发与实现 论文
  • 网站管理登录动漫设计与制作大学
  • 中学网站源码企业网站建设条件
  • 网站附件下载表格怎么做wordpress登录网址
  • 动易网站中添加邮箱成都网络营销策划
  • 做网站1核1g服务器够吗外贸兼职平台
  • 商务网站主页设计公司国产免费erp软件
  • 网站名称去哪里注册中国制造交易网登录
  • 网站建设公司招人建网站软件哪个好
  • 网站域名怎么改化妆品瓶子怎么做网站
  • 郴州网站小程序网站开发包括什么
  • 做钢化膜网站百度中心
  • 易企秀微网站如何做文字链接静态网站设计与制作书籍
  • 网站开发 activex视频网站亏损也做
  • 高档网站模板找大连做企业网站的公司
  • 手机网站 制作校园兼职网站建设
  • 计算机应用技术 网站开发wordpress前大
  • 瑞安网站开发申请网站域名多少钱
  • 做非法网站的有没有外贸公司做网站
  • 公司网站建设全包公司微信网站建设方案
  • h5网站建设+北京wordpress第三方客户端
  • 怎么建一个免费的网站河北网站制作 网站开发
  • 申请备案网站首页seo提权软件
  • 湖州市交通建设管理局网站wordpress备案号居中
  • 自己服务器建网站 备案网站一键制作
  • 做彩妆网站的公司网站制作乛薇