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

网站模板怎么做广告加盟

网站模板怎么做,广告加盟,如何修改wordpress模板文件,wordpress主题知更目录 一、定义接口、实体类、创建XML文件实现接口) 二、MyBatis的增删改查 🍅1、MyBatis传递参数查询 🎈写法一 🎈写法二 🎈两种方式的区别 🍅2、删除操作 🍅3、根据id修改用户名 &#x…

目录

一、定义接口、实体类、创建XML文件实现接口)

二、MyBatis的增删改查 

🍅1、MyBatis传递参数查询

        🎈写法一

         🎈写法二

        🎈两种方式的区别

🍅2、删除操作

🍅3、根据id修改用户名

🍅4、添加用户操作

        🎈返回受影响的行数

        🎈返回自增id

🍅5、like查询

🍅6、多表查询

三、注意

🍅1、mybatisx插件报错

🍅2、数据回滚

🍅 3、查询某字段结果为null时



一、定义接口、实体类、创建XML文件实现接口)

注意包名:

实体类

package com.example.demo.model;import lombok.Data;import java.time.LocalDateTime;@Data
public class UserInfo {private int id;private String username;private String password;private String photo;private LocalDateTime updatatime;private LocalDateTime createtime;private int state;}

接口

package com.example.demo.dao;import com.example.demo.model.UserInfo;
import org.apache.ibatis.annotations.Mapper;import java.util.List;@Mapper  //数据持久层的标志
public interface UserMapper {List<UserInfo> getAll();
}

XML文件 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.demo.dao.UserMapper">
<!--    id是UserMapper的方法名--><select id="getAll" resultType="com.example.demo.model.UserInfo">
--         不写分号select * from userinfo</select>
</mapper>

二、MyBatis的增删改查 

🍅1、MyBatis传递参数查询

        🎈写法一

@Param("id")与${id}相互匹配 (及时执行)

使用id去查询某条数据(传参数)

UserInfo getUserById(@Param("id") Integer uid);
-- 这里应该写${id},而不是uid
<select id="getUserById" resultType="com.example.demo.model.UserInfo">select * from userinfo where id=${id}</select>

测试类: 

@Testvoid getUserById() {UserInfo userInfo = userMapper.getUserById(1);System.out.println(userInfo.toString());}

         🎈写法二

@Param("id")与#{id}相互匹配 (预执行)

<select id="getUserById" resultType="com.example.demo.model.UserInfo">select * from userinfo where id=#{id}</select>

        🎈两种方式的区别

1.${}是直接替换;而#{}是预执行

2.使用${}是不安全的,存在SQL注入;而#{}是安全的,不存在SQL注入

3.${}使用场景:当业务需要传递SQL语句时,只能使用${},不能使用#{}。

SQL注入:将SQL代码插入或添加到应用(用户)的输入参数中的攻击,之后再将这些参数传递给后台的sql服务器加以解析和执行。


🍅2、删除操作

UserInfo getUserById(@Param("id") Integer uid);
<!--    delete操作不需要设置返回类型--><delete id="delById">delete from userinfo where id=#{id}</delete>

 测试类

@Transactional   //数据回滚:使用该注解,会执行下面操作,但是不会真的操作数据库中的内容@Testvoid testGetUserById() {int id = 1;UserInfo result = userMapper.getUserById(id);System.out.println("受影响的行数:"+result);}

🍅3、根据id修改用户名

//    根据id修改用户名
//    返回受影响行数int update(UserInfo userInfo);
<!--    默认返回受影响的行数,不需要设置resultType--><update id="update" >update userinfo set username=#{username} where id=#{id}</update>
@Transactional@Testvoid update() {UserInfo userInfo = new UserInfo();userInfo.setId(1);userInfo.setUsername("管理员");int result = userMapper.update(userInfo);System.out.println("受影响行数"+ result);}

 运行结果:


🍅4、添加用户操作

        🎈返回受影响的行数

//返回受影响字段
int add(UserInfo userInfo);
<insert id="add">insert into userinfo(username,password,photo) 
values (#{username},#{password},#{photo})</insert>
//测试类
@Testvoid add() {UserInfo userInfo = new UserInfo();userInfo.setUsername("张三");userInfo.setPassword("11111");userInfo.setPhoto("/image/default.png");int result = userMapper.add(userInfo);System.out.println("受影响的行数:"+ result);}

 运行结果:

        🎈返回自增id

    int insert(UserInfo userInfo);
<insert id="insert" useGeneratedKeys="true" keyColumn="id" keyProperty="id">insert into userinfo(username,password,photo) values (#{username},#{password},#{photo})</insert>
//测试类 
@Testvoid insert() {UserInfo userInfo = new UserInfo();userInfo.setUsername("李四");userInfo.setPassword("111111");userInfo.setPhoto("");int result = userMapper.insert(userInfo);System.out.println("受影响的行数:" + result +"| id:"+ userInfo.getId());}

 运行结果:

 对应关系:


🍅5、like查询

    List<UserInfo> getListByLike(@Param("username")String username);
<select id="getListByLike" resultType="com.example.demo.model.UserInfo">select * from userinfo where username like concat('%',#{username},'%')</select>
@Testvoid getListByLike() {String username = "三";List<UserInfo> list = userMapper.getListByLike(username);System.out.println(list);}

运行结果:


🍅6、多表查询

使用注解将sql语句和接口连接起来。

之前是,写一个接口就要写一个对应的xml文件编写sql语句。现在可以不使用这种方法

可以直接在接口中通过注解编写查询语句:

@Mapper
public interface ArticleMapper{@Select("select a.*,u.username from articleinfo a" +"left join userinfo u on a.uid=u.id")List<Articleinfo> getAll();
}

测试类:

class ArticleMapperTest {@Autowiredprivate ArticleMapper articleMapper;@Testvoid getAll() {List<Articleinfo> list = articleMapper.getAll();System.out.println(list);}
}

 


三、注意

🍅1、mybatisx插件报错

 以上并非系统报错,而是插件在报错,告诉我们没有设置返回的技术类型,高版本的idea遇到这种情况运行是不会报错的。

🍅2、数据回滚

 @Transactional   //数据回滚:使用该注解,会执行下面操作,但是不会真的操作数据库中的内容

🍅 3、查询某字段结果为null时

原因是:实体类中的属性和数据库表中的字段名不一致。

       解决方案:

        1.将实体类中的属性和表中的字段名保持一致(最简单的解决方案)

        2.使用sql语句中的as进行列名(字段名)重命名,让列名(字段名)等于属性名

<!--   加入要查询的字段名应该是name,对name进行重命名 -->
<selet id="getUserById" resultType="com.example.demo.model.UserInfo">select username as name from userinfo where id=#{id}c</select>

        3.定义一个resultMap,将属性名和字段名进行手动映射

        resultMap放入mapper.xml的<mapper></mapper>中 

<resultMap id="UserMapper" type="com.example.demo.model.UserInfo"><id column="id" property="id"></id><result column="username" property="username"></result><result column="password" property="password"></result></resultMap>
<select id="getUserById" resultMap="BaseMap">select * from userinfo where id=#{id}</select>

以下是对应关系:尽量把全部属性映射上,否则多表查询时可能会报错。

上图有个错误!!!  colum是表里面的字段名,property是实体类里的属性,写反了。

http://www.mmbaike.com/news/73734.html

相关文章:

  • 温州专业微网站制作电话关键词推广软件
  • 怎样做自己的微商网站网络营销师证书有用吗
  • 无锡 网站设计社会新闻热点事件
  • 个人能做网站吗廊坊seo排名收费
  • 大连金州网站建设企业网站设计与推广
  • 古镇建网站公司软文发稿网站
  • 做游戏模型素材 网站b站推广网站入口2023是什么
  • 万江网站建设如何做品牌推广方案
  • 中国摄影师个人网站设计视频号链接怎么获取
  • 人性本私wordpress河北关键词seo排名
  • 网站图片翻页怎么做优秀网站
  • 网站建设的市场容量网络推广接单平台
  • 江西网站开发哪家专业百度广告收费标准
  • 自己的电脑做网站百度指数购买
  • 手机付费咨询网站建设湖南省人民政府官网
  • 怎样做网站 网页个人网站设计成品
  • 手机免费自建网站苏州百度推广公司地址
  • 做网站公司哪家好网络推广的好处
  • 行业内做网站的公司排名网页制作
  • 网站建设报表明细app拉新推广平台代理
  • 网站内容分享无锡做网站的公司
  • 诚讯网站设计网站引流推广怎么做
  • 外贸独立网站seo湘潭网络推广
  • 网页设计与网站建设课程总结国际军事形势最新消息
  • 网站 租用服务器百度快速seo
  • 广告联盟平台排名苏州seo按天扣费
  • 属于网站开发工具的是汕头百度网站排名
  • 快速排名优化推广手机seo关键词优化排名
  • 免费申请二级网站源码自助建站
  • b2b贸易温州seo按天扣费