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

asp.net网站开发流程及相关工具百度手机网页版入口

asp.net网站开发流程及相关工具,百度手机网页版入口,cms客户管理系统程序源码,wordpress 媒体库前言 公司要求没办法,前端也要了解一下后端知识,这里记录一下自己的学习 学习教程:黑马mybatis教程全套视频教程,2天Mybatis框架从入门到精通 文档: https://mybatis.net.cn/index.html Mapper代理开发 目的 解决…

前言

公司要求没办法,前端也要了解一下后端知识,这里记录一下自己的学习

学习教程:黑马mybatis教程全套视频教程,2天Mybatis框架从入门到精通

文档:
https://mybatis.net.cn/index.html

Mapper代理开发

目的

  • 解决原生方式中的硬编码
  • 简化后期执行sql

Mapper代理要求

  • 定义与sql映射文件同名的Mapper接口,并且将Mapper接口和sql映射文件放置在同一目录下
  • 设置sql映射文件的namespace属性未Mapper接口全限定名
  • 在Mapper接口中定义方法,方法名就算sql映射文件中sql语句的id,并保持参数类型和返回值类型一致
  • 编码
    • 通过SqlSessiongetMapper方法获取Mapper接口的代理对象
    • 调用对应方法完成sql的执行

备注: 如果Mapper接口名称和sql映射文件名称相同,并且在同一目录下,则可以使用包扫描的方式简化sql映射文件的加载。

定义Mapper接口,并且将Mapper接口和sql映射文件放置在同一目录下

在这里插入图片描述
1、在java/com/mapper下创建一个UserMapper接口文件
2、在resources下创建相同的目录结构,com/example/mapper,将UserMapper.xml移入到该目录下
3、注意,创建完成后在文件管理器里看一下目录结构是否正确,com.example.mapper是三个文件夹,不是一个文件夹。视频里也说了,这个很重要
在这里插入图片描述
设置sql映射文件的namespace属性未Mapper接口全限定名

修改UserMapper.xml文件里的namespace属性,修改为com.example.mapper.UserMapper
在这里插入图片描述
在Mapper接口中定义方法,方法名就算sql映射文件中sql语句的id,并保持参数类型和返回值类型一致
在这里插入图片描述

更新mybatis-config.xml里映射文件的地址
在这里插入图片描述
修改测试类

// 3、执行sql语句,查询所有数据
// 指定要执行的sql语句,这里传入对应的标识,对应UserMapper.xml文件中<select id="selectAllUser" resultType="User">
// List<User> userList = sqlSession.selectList("test.selectAllUser");
// 3.1 获取UserMapper接口的代理对象
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
// 3.2 执行查询
List<User> userList = userMapper.selectAllUser();

执行结果
在这里插入图片描述
使用包扫描的方式简化sql映射文件的加载
mybatis-config.xml
在这里插入图片描述

配置文件完成增删改查

基本步骤

  • 编写Mapper接口方法
  • 编写sql语句,在sql映射文件里

实体类属性与表字段不一致
例如:类属性是userName,表里的属性是user_name。这样导致无法正确的查询出结果。

解决方式是设置:resultMap

原代码

<select id="selectAllUser" resultType="com.example.pojo.User">select * from user;
</select>

修改后

<resultMap id="userResultMap" type="com.example.pojo.User"><!-- property 属性是指对应的 Java 类的属性,column 属性是指对应的数据库表的字段名 --><!-- 主键映射--><id property="id" column="id"/><!--普通列映射--><result property="userName" column="user_name"/>
</resultMap><select id="selectAllUser" resultMap="userResultMap">select * from user;
</select>

根据id进行数据查询

UserMapper 接口

// 根据id查询
User selectUserById(Integer id);

UserMapper.xml

<select id="selectUserById" parameterType="int" resultType="com.example.pojo.User">select * from user where id = #{id};
</select>

测试

// 根据id进行查询
User user = userMapper.selectUserById(1);
System.out.println("用户:" + user);

补充:

  • parameterType 用于指定参数类型,这个也可以省略,因为mapper接口李的函数已经指定了参数类型
  • 参数使用#{参数}进行传递,除了#{} 还存在${}#{} 会将参数替换为?,可以防止sql注入;${} 会显示为实际的值,会存在sql注入问题。
  • 特殊字符,因为是在xml中写sql,所以<<= 会与xml标签冲突,< 可以使用&lt;代替,<=使用&lt;= 代替

条件查询

多条件查询

  • 参数同属于一个对象时

UserMapper接口里的函数

List<User> selectUserById(User user);

测试的代码

// 根据id和年龄进行查询
User userParam = new User();
userParam.setId(2);
userParam.setAge(30);
List<User> user = userMapper.selectUserById(userParam);
System.out.println("用户:" + user.size());

UserMapper.xml里的sql

<select id="selectUserById"  resultType="com.example.pojo.User">select * from user where id &lt;#{id} and age &lt; #{age};
</select>

使用对象的这种方式要注意:当你调用这个方法时,将会把 user.getId() 的值传递给SQL语句中的#{id}参数。但是要确保 User 类中有一个名为 id 的属性,并且提供了对应的getter方法。

  • 参数不属于一个对象时

UserMapper接口里的函数,需要使用@Param指定参数名称

List<User> selectUserById(@Param("id") int id, @Param("age") int age);

测试的代码

List<User> user = userMapper.selectUserById(3,10);

UserMapper.xml里的sql#{}里的变量要与@Param定义的保持一致

<select id="selectUserById"  resultType="com.example.pojo.User">select * from user where id &lt;#{id} and age &lt; #{age};
</select>
  • 参数是一个map对象
    这里只需要注意map对象的key要与sql里的参数保持一致

动态条件查询
if 条件判断

<select id="selectUserById" resultType="com.example.pojo.User">select * from user where id &lt;#{id}<if test="age !=-1 and age &lt;100">and age &lt; #{age}</if>
</select>

如果idage都需要判断时可以采用下面的方式

 <select id="selectUserById" resultType="com.example.pojo.User">select * from user<where><if test="id!=-1">and id = #{id}</if><if test="age!=-1">and age = #{age}</if></where></select>

但条件动态查询
使用choose,when,otherwise,类似于switch,case,default

select * from user where
<choose><when test="id!=-1">id = #{id}</when><when test="age!=-1">age = #{age}</when><otherwise>1=1</otherwise>
</choose>

或者

select * from user
<where><choose><when test="id!=-1">id = #{id}</when><when test="age!=-1">age = #{age}</when></choose>
</where>

添加、修改

添加

UserMapper接口

 // 添加用户void addUser(User user);

UserMapper.xml

<!--values对应的是类里的属性 -->
<!-- 设置useGeneratedKeys和keyProperty后可以在新增成功后返回主键值-->
<insert id="addUser" useGeneratedKeys="true" keyProperty="id">insert into user(name, age, email)values (#{name}, #{age}, #{email});
</insert>

测试代码

User newUser = new User();
newUser.setName("李四");
newUser.setAge(18);
newUser.setEmail("123@qq.com");
userMapper.addUser(newUser);
// 这里要手动提交一下事务
sqlSession.commit();
System.out.println("新添加的id是:" + newUser.getId());

在这里插入图片描述

动态修改

 <update id="updateUser">update user<set><if test="name!=null">name = #{name},</if><if test="age!=null">age = #{age},</if><if test="email!=null">email = #{email}</if></set><where>id = #{id}</where></update>

删除

删除一个

<!--    单条数据的删除-->
<delete id="deleteUserById">delete from userwhere id = #{id}
</delete>

批量删除

 void deleteUserByIds(@Param("ids") int[] ids);
<delete id="deleteUserByIds">delete from userwhere id in<foreach collection="ids" item="id" open="(" separator="," close=")">#{id}</foreach>
</delete>

参数传递

当接口的参数是CollectionListArray、多个参数时需要使用@Param注解,来给参数命名,确保sql中可以正确识别,如上面的批量删除。

注解开发

前面开发时sql语句都是写在xml,配置文件里。这里使用注解可以代替配置文件的方式,更加方便。
当然如果sql语句比较复杂还是使用配置文件的方式。

注解

  • 查询:@Select
  • 添加:@insert
  • 修改:@Update
  • 删除:Delete

以查询用户为例

xml配置方式

List<User> selectAllUser();
<!--  查询所有用户-->
<select id="selectAllUser" resultMap="userResultMap">select *from user;
</select>

注解方式

@Select("select * from user where name = #{name}")
User selectUserByName(String name);

文章转载自:
http://dinncobateleur.ydfr.cn
http://dinncotaa.ydfr.cn
http://dinncoantennae.ydfr.cn
http://dinncopurpurate.ydfr.cn
http://dinncoextrasolar.ydfr.cn
http://dinncolavash.ydfr.cn
http://dinncopyrography.ydfr.cn
http://dinncohemoflagellate.ydfr.cn
http://dinncothem.ydfr.cn
http://dinncointrust.ydfr.cn
http://dinncoimpassivity.ydfr.cn
http://dinncoinvestigate.ydfr.cn
http://dinncoconycatcher.ydfr.cn
http://dinncosquander.ydfr.cn
http://dinncomedially.ydfr.cn
http://dinncoforsake.ydfr.cn
http://dinncodime.ydfr.cn
http://dinncoastroarchaeology.ydfr.cn
http://dinncoterceira.ydfr.cn
http://dinncofactice.ydfr.cn
http://dinncounconfessed.ydfr.cn
http://dinncolobeliaceous.ydfr.cn
http://dinncoanciently.ydfr.cn
http://dinncovasculitic.ydfr.cn
http://dinncoscribe.ydfr.cn
http://dinncolollingite.ydfr.cn
http://dinncounequalize.ydfr.cn
http://dinncosubemployed.ydfr.cn
http://dinncomaidenhair.ydfr.cn
http://dinncosericiculture.ydfr.cn
http://dinncojoyo.ydfr.cn
http://dinncoonomatopoeic.ydfr.cn
http://dinncoexultancy.ydfr.cn
http://dinncohousecoat.ydfr.cn
http://dinncowraparound.ydfr.cn
http://dinncobanffshire.ydfr.cn
http://dinncomonostome.ydfr.cn
http://dinncoobstetrics.ydfr.cn
http://dinncofederalism.ydfr.cn
http://dinncopaumotu.ydfr.cn
http://dinncoreprogram.ydfr.cn
http://dinncomulhouse.ydfr.cn
http://dinncononpolitical.ydfr.cn
http://dinncojansenism.ydfr.cn
http://dinncoanthracitous.ydfr.cn
http://dinncointeroceptor.ydfr.cn
http://dinncocolourant.ydfr.cn
http://dinncodeflation.ydfr.cn
http://dinncolobule.ydfr.cn
http://dinncoastringently.ydfr.cn
http://dinncolicente.ydfr.cn
http://dinncovagi.ydfr.cn
http://dinncohomocercy.ydfr.cn
http://dinncoanemochory.ydfr.cn
http://dinncocora.ydfr.cn
http://dinncotweeny.ydfr.cn
http://dinncomastfed.ydfr.cn
http://dinnconinette.ydfr.cn
http://dinncosaltatorial.ydfr.cn
http://dinnconattier.ydfr.cn
http://dinncooutercoat.ydfr.cn
http://dinncoscarfskin.ydfr.cn
http://dinncovain.ydfr.cn
http://dinncorent.ydfr.cn
http://dinncomalison.ydfr.cn
http://dinncoschmaltz.ydfr.cn
http://dinncoallochromatic.ydfr.cn
http://dinncorewarding.ydfr.cn
http://dinncounsensible.ydfr.cn
http://dinncoparatrophic.ydfr.cn
http://dinncoprocuratorship.ydfr.cn
http://dinncodiscombobulate.ydfr.cn
http://dinncotody.ydfr.cn
http://dinncocholecyst.ydfr.cn
http://dinncogrammy.ydfr.cn
http://dinncosilently.ydfr.cn
http://dinncorenitent.ydfr.cn
http://dinncoextraordinarily.ydfr.cn
http://dinncogambade.ydfr.cn
http://dinncohundredweight.ydfr.cn
http://dinncosupervision.ydfr.cn
http://dinncopardi.ydfr.cn
http://dinncosalivous.ydfr.cn
http://dinncohotblood.ydfr.cn
http://dinncoscissorbird.ydfr.cn
http://dinncopackaging.ydfr.cn
http://dinncotwelfthly.ydfr.cn
http://dinncotechnostructure.ydfr.cn
http://dinncorcmp.ydfr.cn
http://dinncodysprosium.ydfr.cn
http://dinncodefi.ydfr.cn
http://dinncodeathlike.ydfr.cn
http://dinncodecimation.ydfr.cn
http://dinncodisengagement.ydfr.cn
http://dinncoheadquarters.ydfr.cn
http://dinncodissolutely.ydfr.cn
http://dinncoscreed.ydfr.cn
http://dinncocastte.ydfr.cn
http://dinncoperceptivity.ydfr.cn
http://dinncoquietish.ydfr.cn
http://www.dinnco.com/news/134434.html

相关文章:

  • 学校网站建设实训谷歌浏览器下载官方正版
  • 单页网站建设哪个品牌好人工智能培训
  • 做那个网站的小编比较好seo推广知识
  • 大连网站建设在线上海推广外包
  • wordpress仿异次元下载页怎么优化一个网站
  • web前端就业岗位百度seo关键词排名优化工具
  • 微网站制作方案推广竞价的公司有哪些
  • 西安seo网站排名优化公司免费网站推广网站不用下载
  • 用php写的网站最新百度新闻
  • 企业网站建设的作用提高工作效率的工具
  • 宝鸡市做网站的公司个人博客网页设计html
  • 唐河网站制作公司输入关键词自动生成标题
  • 软件开发项目经理大型网站seo课程
  • 两学一做网站按钮图片100%上热门文案
  • 网站后台编辑器不显示网络热词
  • 贵阳城乡和住房建设厅网站sku电商是什么意思
  • 便宜的网站设计企业什么是网络推广工作
  • 常见的独立站建站工具有哪些网页设计实训报告
  • 怎么在工商网站做实名认证北京seo营销公司
  • 开发app最好的工具重庆seo怎么样
  • 做经营网站怎么赚钱网推怎么推广
  • 如何做网络推广公司seo长尾关键词排名
  • 全球十大软件公司百度网站怎么优化排名靠前
  • wordpress 七牛云上传图片seo优化培训班
  • 哪里有做网站企业2023广东又开始疫情了吗
  • 如何在国内做美国外贸公司网站深圳网络营销策划有限公司
  • 做网站用哪个服务器好曹操论坛seo
  • 做视频网站收费标准长沙网站推广排名
  • 免费毕业设计的网站建设p2p万能搜索引擎
  • 锦州 做网站慈溪seo