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

电子商务网站建设资讯qq空间刷赞推广网站

电子商务网站建设资讯,qq空间刷赞推广网站,枣庄seo推广,深圳网站制作公司怎么样首先介绍此次使用的数据库结构,然后引出注意事项。 通过基于xml和基于注解的方式分别实现了增删改查,还有获取参数值、返回值的不同类型对比,帮助大家一次性掌握两种代码编写能力。 目录 数据库 数据库表 实体类 对应的实体类如下&#x…

首先介绍此次使用的数据库结构,然后引出注意事项。

通过基于xml和基于注解的方式分别实现了增删改查,还有获取参数值、返回值的不同类型对比,帮助大家一次性掌握两种代码编写能力。

目录

数据库

数据库表

实体类

对应的实体类如下:

注意事项

1)两种占位符的说明

①#{}

②${}

2)增、删、改的返回值说明

3)查询操作

①必须指定resultType或是resultMap

4)对应关系

①数据库名与类名不一致的情况

②字段名与属性名不一致的情况

1.获取参数值的方式

1.1基于xml

1.1.1单个参数

①mapper

②xml

③test

④结果

1.1.2多个参数

①mapper

②xml

写法一

写法二

③test

④结果

1.1.3map参数

①mapper

②xml

③test

④结果

1.1.4实体类参数

①mapper

②xml

③test

④结果

1.1.5使用@Param标识参数

①mapper

写法一

写法二

错误写法

②xml

③test

④结果

1.2基于注解

1.2.1单个参数

①mapper

写法一

写法二

②test

③结果

1.2.2多个参数

①mapper

 ②test

③结果

1.2.3map参数

①mapper

②test

③结果

1.2.4实体类参数

①mapper

②test

③结果

1.2.5使用@Param标识参数

①mapper

写法一

写法二

②test

③结果

2.各种操作功能

2.1基于xml

2.1.1增

①mapper

②mapper.xml

③test

④结果

主键id自增并返回值

mapper

mapper.xml

test

结果

2.1.2删

①mapper

②mapper.xml

③test

④结果

删除前

删除后

2.1.3改

①mapper

②mapper.xml

③test

④结果

修改前

修改后

2.1.4查

①mapper

②mapper.xml

③test

④结果

2.2基于注解

2.2.1增

①mapper

②test

③结果

主键id自增并返回结果

mapper

test

结果

2.2.2删

①mapper

②test

③结果

删除前

删除后

2.2.3改

①mapper

②test

③结果

修改前

修改后

2.2.4查

①mapper

②test

③结果

3.各种返回值类型

3.1基于xml

①查询单条数据

②查询多条数据

3.2基于注解

①查询单条数据

②查询多条数据


数据库

数据库表

此次实验的数据库表结构如下,包含主键id和三个属性

实体类

对应的实体类如下:

import com.baomidou.mybatisplus.annotation.TableName;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;/*** ClassName: Book* Package: com.ykx.domain* Description: mybatis学习的数据库表tbl_book对应的实体类*/
@Data
@AllArgsConstructor
@NoArgsConstructor
@TableName("tbl_book")
public class Book {private Integer id;private String type;private String name;private String description;
}

注意事项

1)两种占位符的说明

①#{}

传入的参数当成一个字符串,会给传入的参数加单引号

能够很大程度上防止sql注入

一般用于替换某个值

②${}

将传入的参数值直接显示生成在sql中,不会自动加引号

预编译之前就已经被变量替换,无法防止sql注入

一般用于替换表、字段名

2)增、删、改的返回值说明

返回值固定位Integer,表示受影响的行数

3)查询操作

①必须指定resultType或是resultMap

不配置别名的时候,值为类的全类名

返回值是集合的时候,只需指定集合的泛型即可,如

List<Book> getAll();<select id="getAll" resultType="com.ykx.pojo.Book">select * from tbl_book
</select>

4)对应关系

①数据库名与类名不一致的情况

②字段名与属性名不一致的情况

1.获取参数值的方式

MyBatis 提供了多种传递参数的方式到 SQL 。

理解参数传递的机制有助于提高代码的可读性、灵活性。

1.1基于xml

1.1.1单个参数

①mapper
Book getById(Integer id);
②xml

在单个字面量参数的情况下,{}内的名称可以任意取

如下面的代码可以把#{id} 改成 #{ID}亦或是其他

<select id="getById" resultType="com.ykx.domain.Book">select * from tbl_book where id = #{id}
</select>
③test
@Test
public void testSelect(){Book book = bookMapperXML.getById(1);System.out.println(book);
}
④结果

1.1.2多个参数

①mapper
Book check(String type,String name);
②xml
写法一
<select id="check" resultType="com.ykx.domain.Book">select * from tbl_book where type = #{type} and name = #{name}
</select>
写法二
<select id="check" resultType="com.ykx.domain.Book">select * from tbl_book where type = #{param1} and name = #{param2}
</select>
③test
@Test
public void testCheck(){Book book = bookMapperXML.check("java","mybatis数据");System.out.println(book);
}
④结果

1.1.3map参数

①mapper
Book checkByMap(Map<String,String> map);
②xml
<select id="checkByMap" resultType="com.ykx.domain.Book">select * from tbl_book where type = #{type} and name = #{name}
</select>
③test
@Test
public void testCheckByMap(){Map<String,String> map = new HashMap<>();map.put("type","java");map.put("name", "mybatis数据");Book book = bookMapperXML.checkByMap(map);System.out.println(book);
}
④结果

1.1.4实体类参数

①mapper
Integer insert(Book book);
②xml
<insert id="insert">insert into tbl_book (type,name,description) values(#{type},#{name},#{description})
</insert>
③test
@Test
public void testInsert(){Book book = new Book(null,"计算机视觉","三维重建","基于深度学习的三维重建");Integer ans = bookMapperXML.insert(book);System.out.println("受影响的行数:" + ans);
}
④结果

1.1.5使用@Param标识参数

 在字段名和属性名对不上或是参数名和字段名对不上的时候很有用

①mapper
写法一
Book check(@Param("type") String type, @Param("name") String name);
写法二
Book check(@Param("type") String type2, @Param("name") String name2);
错误写法
Book check(String type2, String name2);
②xml
<select id="check" resultType="com.ykx.domain.Book">select * from tbl_book where type = #{type} and name = #{name}
</select>
③test
@Test
public void testCheck(){Book book = bookMapperXML.check("java","mybatis数据");System.out.println(book);
}
④结果

1.2基于注解

1.2.1单个参数

①mapper

单个参数的情况下,接口的参数名和注解里的sql语句参数可以不一致

写法一
@Select("select * from tbl_book where id = #{id}")
Book getById(Integer id);
写法二
@Select("select * from tbl_book where id = #{aa}")
Book getById(Integer id);
②test
@Test
public void testSelect(){Book book = bookMapper.getById(78);System.out.println(book);
}
③结果

1.2.2多个参数

①mapper

注:在未使用@Param的情况下,参数名不对应或是顺序不一样会导致查询失败

@Select("select * from tbl_book where type = #{type} and name = #{name}")
Book getOne(String type,String name);
 ②test
@Test
public void testGetOne(){Book book = bookMapper.getOne("计算机视觉","三维重建");System.out.println(book);
}
③结果

1.2.3map参数

①mapper
@Select("select * from tbl_book where type = #{type} and name = #{name}")
Book getByMap(Map<String, String> map);
②test

注:map里的key值要和#{}里的值对应,否则出现查询失败的情况

@Test
public void testByMap(){Map<String,String> map = new HashMap<>();map.put("type","java");map.put("name", "mybatis数据");Book book = bookMapper.getByMap(map);System.out.println(book);
}
③结果

1.2.4实体类参数

①mapper
@Insert("insert into tbl_book (id,type,name,description) " +"values(#{id},#{type},#{name},#{description})")
Integer insert(Book book);
②test
@Test
public void testInsert(){Book book = new Book(null,"新增数据","测试新增","不带id的新增测试");Integer ans = bookMapper.insert(book);System.out.println("受影响的行数:" + ans);
}
③结果

1.2.5使用@Param标识参数

只需要#{}里的值和@Param()里的值一样就行

①mapper
写法一
@Select("select * from tbl_book where type = #{type} and name = #{name}")
Book getOne(@Param("type") String type,@Param("name") String name);
写法二
@Select("select * from tbl_book where type = #{type} and name = #{name}")
Book getOne(@Param("type") String typeaa,@Param("name") String nameaa);
②test
@Test
public void testGetOne(){Book book = bookMapper.getOne("计算机视觉","三维重建");System.out.println(book);
}
③结果

2.各种操作功能

2.1基于xml

2.1.1增

①mapper
//增:实体对象新增数据
Integer insert(Book book);
②mapper.xml
<insert id="insert">insert into tbl_book (id,type,name,description) values(#{id},#{type},#{name},#{description})
</insert>

:这里设置了id主键自增的话,可以不设置和传入id

③test
//增:实体对象新增数据
@Test
public void testInsert(){Book book = new Book(71,"计算机视觉","三维重建","基于深度学习的三维重建");Integer ans = bookMapperXML.insert(book);System.out.println("受影响的行数:" + ans);
}
④结果

主键id自增并返回值

关键点:在xml里需要带上useGeneratedKeys和keyProperty

mapper
Integer autoId(Book book);
mapper.xml
<insert id="autoId" useGeneratedKeys="true" keyProperty="id">insert into tbl_book (type,name,description) values(#{type},#{name},#{description})
</insert>
test
//增:主键自增且返回值
@Test
public void testAutoId(){Book book = new Book(null,"测试id自增","返回主键id","是否成功获取id的值");Integer ans = bookMapperXML.autoId(book);System.out.println("受影响的行数:" + ans);System.out.println("获取主键自增的id值:" + book.getId());
}
结果

2.1.2删

①mapper
//删:根据id删除数据
Integer delete(Integer id);
②mapper.xml
<delete id="delete">delete from tbl_book where id = #{id};
</delete>
③test
//删:根据id删除数据
@Test
public void testDelete(){Integer ans = bookMapperXML.delete(68);System.out.println("受影响的行数:" + ans);
}
④结果

删除前

删除后

2.1.3改

①mapper
//改:根据id修改数据
Integer update(Book book);
②mapper.xml
<update id="update">update tbl_book set type = #{type},name = #{name},description = #{description} where id = #{id};
</update>
③test
//改:根据id修改数据
@Test
public void testUpdate(){Book book = new Book(58,"测试修改","mybatis修改","基于xml的修改操作");Integer ans = bookMapperXML.update(book);System.out.println("受影响的行数:" + ans);
}
④结果

修改前

修改后

2.1.4查

①mapper
@Mapper
public interface BookMapperXML {//查:根据id查询数据Book getById(Integer id);//查:查询所有数据List<Book> getAll();}
②mapper.xml
<?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.ykx.mapper.BookMapperXML"><select id="getById" resultType="com.ykx.domain.Book">select * from tbl_book where id = #{id}</select><select id="getAll" resultType="com.ykx.domain.Book">select * from tbl_book</select>
</mapper>
③test
@SpringBootTest
public class XMLMybatisTest {@Autowiredprivate BookMapperXML bookMapperXML;//查:根据id查询数据@Testpublic void testSelect(){Book book = bookMapperXML.getById(1);}//查:查询所有数据@Testpublic void testGetAll(){List<Book> books = bookMapperXML.getAll();for(Book book : books){System.out.println(book);}}}
④结果

2.2基于注解

2.2.1增

①mapper
@Insert("insert into tbl_book (id,type,name,description) " +"values(#{id},#{type},#{name},#{description})")
Integer insert(Book book);
②test
//增:实体对象新增数据
@Test
public void testInsert(){Book book = new Book(11,"新增数据","测试新增","带id的新增测试");Integer ans = bookMapper.insert(book);System.out.println("受影响的行数:" + ans);
}
③结果

主键id自增并返回结果

关键点:在mapper对应的方法上面加上@Options注解,并设置useGeneratedKeys和keyProperty的属性值。这个只能搭配insert语句使用!

mapper
@Options(useGeneratedKeys = true,keyProperty = "id")
@Insert("insert into tbl_book (type,name,description) " +"values(#{type},#{name},#{description})")
Integer autoId(Book book);
test
//增:主键自增且返回值
@Test
public void testAutoId(){Book book = new Book(null,"新增数据2","测试新增2","不带id的新增测试");Integer ans = bookMapper.autoId(book);System.out.println("受影响的行数:" + ans);System.out.println("获取主键自增的id值:" + book.getId());
}
结果

2.2.2删

①mapper
//删:根据id删除数据
@Delete("delete from tbl_book where id = #{id}")
Integer delete(Integer id);
②test
//删:根据id删除数据
@Test
public void testDelete(){Integer ans = bookMapper.delete(8);System.out.println("受影响的行数:" + ans);
}
③结果

删除前

删除后

2.2.3改

①mapper
//改:根据id修改数据
@Update("update tbl_book set type = #{type}, name = #{name}, description = #{description} where id = #{id}")
Integer update(Book book);
②test
//改:根据id修改数据
@Test
public void testUpdate(){Book book = new Book(62,"测试修改","mybatis修改","基于注解的修改操作");Integer ans = bookMapper.update(book);System.out.println("受影响的行数:" + ans);
}
③结果
修改前

修改后

2.2.4查

①mapper
//查:根据id查询数据
@Select("select * from tbl_book where id = #{id}")
Book getById(Integer id);//查:查询所有数据
@Select("select * from tbl_book")
List<Book> getAll();
②test
//查:根据id查询数据
@Test
public void testSelect(){Book book = bookMapper.getById(71);System.out.println(book);
}//查:查询所有数据
@Test
public void testGetAll(){List<Book> books = bookMapper.getAll();for(Book book : books){System.out.println(book);}
}
③结果

3.各种返回值类型

对于增、删、改操作一般返回值类型为integer,表示数据表受影响的行数。对于查询操作,一般返回值类型为实体类或是集合类型。

前面已经对增、删、改返回integer进行了测试,这里就不再赘述,下面对查询操作的返回值进行一个总结。

3.1基于xml

①查询单条数据

mapper层接口用对应的实体类接收

//查:根据id查询数据
Book getById(Integer id);

xml文件设置resultType为具体的实体类类型

<select id="getById" resultType="com.ykx.domain.Book">select * from tbl_book where id = #{id}
</select>

②查询多条数据

mapper层接口用List集合接收,其泛型为对应的实体类类型

//查:查询所有数据
List<Book> getAll();

xml文件设置resultType为具体的实体类类型

<select id="getAll" resultType="com.ykx.domain.Book">select * from tbl_book
</select>

3.2基于注解

①查询单条数据

mapper层接口的返回值类型设置为对应的实体类类型即可

//查:根据id查询数据
@Select("select * from tbl_book where id = #{id}")
Book getById(Integer id);

②查询多条数据

mapper层接口的返回值类型设置为List集合,其泛型为对应的实体类类型即可

//查:查询所有数据
@Select("select * from tbl_book")
List<Book> getAll();

原创内容 未经同意禁止转载 如有引用请标明出处


文章转载自:
http://bachelordom.c7497.cn
http://hutted.c7497.cn
http://soak.c7497.cn
http://labroid.c7497.cn
http://morphemics.c7497.cn
http://intelligible.c7497.cn
http://astration.c7497.cn
http://danseur.c7497.cn
http://restively.c7497.cn
http://prehistoric.c7497.cn
http://merrymaker.c7497.cn
http://teleradium.c7497.cn
http://phytobenthon.c7497.cn
http://sbw.c7497.cn
http://festoonery.c7497.cn
http://provender.c7497.cn
http://speedwriting.c7497.cn
http://bailer.c7497.cn
http://baseball.c7497.cn
http://vociferously.c7497.cn
http://disenroll.c7497.cn
http://challie.c7497.cn
http://sweathog.c7497.cn
http://caravanserai.c7497.cn
http://shemozzle.c7497.cn
http://brutalitarian.c7497.cn
http://gastrinoma.c7497.cn
http://empirically.c7497.cn
http://tomentose.c7497.cn
http://hematometer.c7497.cn
http://pentadactyl.c7497.cn
http://winsome.c7497.cn
http://adjudgment.c7497.cn
http://yankeefy.c7497.cn
http://hierurgy.c7497.cn
http://affirmatively.c7497.cn
http://lithemic.c7497.cn
http://frikadel.c7497.cn
http://loir.c7497.cn
http://polygamical.c7497.cn
http://stationer.c7497.cn
http://irrevocably.c7497.cn
http://kitling.c7497.cn
http://hybrimycin.c7497.cn
http://homogenous.c7497.cn
http://roaster.c7497.cn
http://bornite.c7497.cn
http://pulchritude.c7497.cn
http://comely.c7497.cn
http://spirally.c7497.cn
http://ritualise.c7497.cn
http://buttinsky.c7497.cn
http://blind.c7497.cn
http://complaisance.c7497.cn
http://tympana.c7497.cn
http://testee.c7497.cn
http://copybook.c7497.cn
http://accusingly.c7497.cn
http://roomful.c7497.cn
http://katydid.c7497.cn
http://distillable.c7497.cn
http://usom.c7497.cn
http://shiloh.c7497.cn
http://cardiography.c7497.cn
http://orthocephalous.c7497.cn
http://cystamine.c7497.cn
http://cracow.c7497.cn
http://cigaret.c7497.cn
http://cankered.c7497.cn
http://bootlast.c7497.cn
http://speechcraft.c7497.cn
http://papillose.c7497.cn
http://screenland.c7497.cn
http://aerialist.c7497.cn
http://britannic.c7497.cn
http://dramalogue.c7497.cn
http://pretoria.c7497.cn
http://unexpanded.c7497.cn
http://slipware.c7497.cn
http://improvability.c7497.cn
http://renewal.c7497.cn
http://heronsbill.c7497.cn
http://plyers.c7497.cn
http://photocopy.c7497.cn
http://ferned.c7497.cn
http://echelette.c7497.cn
http://pintano.c7497.cn
http://codger.c7497.cn
http://selflessly.c7497.cn
http://vagrancy.c7497.cn
http://serbonian.c7497.cn
http://empiricism.c7497.cn
http://assortment.c7497.cn
http://pawk.c7497.cn
http://midwife.c7497.cn
http://docker.c7497.cn
http://scyphi.c7497.cn
http://tacet.c7497.cn
http://xyphoid.c7497.cn
http://stellated.c7497.cn
http://www.zhongyajixie.com/news/71038.html

相关文章:

  • 天河公司网站建设百度输入法下载
  • wordpress 获得文章的类别seo关键词外包公司
  • 昆明网页制作开发安卓优化大师下载安装
  • 响应式企业营销型网站多少钱企业培训课程视频
  • 杭州 平台 公司 网站建设专业seo网络推广
  • 金融网站制作站长查询工具
  • 成都专业做网站公司广州seo顾问服务
  • 网站架构设计师就业指导seo顾问张智伟
  • 哪个公司的网络最好用广州网站优化推广方案
  • 中山河北建设信息网站西安发布最新通知
  • vi设计策划公司超级优化
  • 互联网营销师报名费西安专业seo
  • 中企动力网站建设公司semikron
  • 牟平建设企业网站什么叫友情链接
  • 专做正品 网站2345网址中国最好
  • 高端服装品牌排行榜搜索引擎的优化方法
  • 做网站的域名和空间是什么意思小说推广平台有哪些
  • 如何做网站费用多少seo代码优化包括哪些
  • 校园在线网站怎么做seo教程搜索引擎优化入门与进阶
  • 哈尔滨建站公司模板站长工具官网
  • 视频网站费用跨境电商
  • 做网站去除视频广告经典的软文广告
  • 封面上的网站怎么做的石家庄网络推广
  • 做 58 那样的网站友情链接平台赚钱吗
  • 微企申请网站百度搜索资源
  • 网站可以做匿名聊天吗中国十大互联网公司排名
  • 手机网站 微信链接成都最新热门事件
  • 婚纱摄影网站的设计思路网络引流怎么做啊?
  • 简单网站开发实例教程如何做好网络营销管理
  • 做门户网站那个系统好网站制作网站推广