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

电商网页设计网站做传销网站

电商网页设计网站,做传销网站,福田蒙派克4s店,wordpress安装nextapp写mybatis代码的方法有两种: 注解xml方式 本篇就介绍XML的方式 使用XML来配置映射语句能够实现复杂的SQL功能,也就是将sql语句写到XML配置文件中。 目录 一、配置XML文件的路径,在resources/mapper 的目录下 二、写持久层代码 1.添加mappe…

写mybatis代码的方法有两种:

  1. 注解
  2. xml方式

本篇就介绍XML的方式

 使用XML来配置映射语句能够实现复杂的SQL功能,也就是将sql语句写到XML配置文件中。

目录

一、配置XML文件的路径,在resources/mapper 的目录下

 二、写持久层代码

1.添加mapper接口

2.添加UserInfoXMLMapper.xml

三、增删改查操作

1.增(Insert)

2.删(Delete)

3.改(Update)

4.查(Select)


 

Mybatis XML 的方式需要以下步骤:

一、配置XML文件的路径,在resources/mapper 的目录下

application.yml文件的配置如下

mybatis:mapper-locations: classpath:mapper/**Mapper.xml

 application.properties⽂件的配置如下

# 配置 mybatis xml 的⽂件路径,在 resources/mapper 创建所有表的 xml ⽂件
mybatis.mapper-locations=classpath:mapper/**Mapper.xml

 二、写持久层代码

持久代码分两部分

  1. 方法定义:Interface
  2. 方法实现:XXX.xml

1.添加mapper接口

数据持久层的接口定义:

import com.example.demo.model.UserInfo;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface UserInfoXMlMapper {List<UserInfo> queryAllUser();
}

2.添加UserInfoXMLMapper.xml

数据持久层的实现,mybatis的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.mapper.UserInfoMapper"></mapper>
  • 创建UserInfoXMLMapper.xml, 路径参考yml中的配置,注意路径和格式要对应,不然会出错。

  •  在XML文件中添加sql语句,查询所有用户的具体实现:
<?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.mapper.UserInfoXMlMapper"><select id="queryAllUser" resultType="com.example.demo.model.UserInfo">select username,`password`, age, gender, phone from user_info</select>
</mapper>

补充:

  • namespace里面的是全限定类名,表示要实现哪个接口 。
  • id为方法名
  • 给访问接口添加单元测试:
@SpringBootTest
class UserInfoMapperTest {@Autowiredprivate UserInfoMapper userInfoMapper;@Testvoid queryAllUser() {List<UserInfo> userInfoList = userInfoMapper.queryAllUser();System.out.println(userInfoList);}
}

运行测试方法即可。


三、增删改查操作

1.增(Insert)

UserInfoMapper 接口

Integer insertUser(UserInfo userInfo);

UserInfoMapper.xml 实现:

<insert id="insertUser">insert into userinfo (username, `password`, age, gender, phone) values (#
{username}, #{password}, #{age},#{gender},#{phone})
</insert>
  • 如果使用@Param 设置参数名的话,使用方法和注解类似。

UserInfoMapper 接口:

Integer insertUser(@Param("userInfo") UserInfo userInfo);

UserInfoMapper.xml 实现:

<insert id="insertUser">insert into user_info (username, `password`, age, gender, phone) values(#{userInfo.username},#{userInfo.password},#{userInfo.age},#
{userInfo.gender},#{userInfo.phone})
</insert>
  • 返回自增id:

接口定义不变,Mapper.xml实现设置设置useGeneratedKeys 和keyProperty属性。

<insert id="insertUser" useGeneratedKeys="true" keyProperty="id">insert into user_info (username, `password`, age, gender, phone) values(#{userInfo.username},#{userInfo.password},#{userInfo.age},#
{userInfo.gender},#{userInfo.phone})
</insert>

2.删(Delete)

UserInfoMapper接口:

Integer deleteUser(Integer id);

UserInfoMapper.xml 实现:

<delete id="deleteUser">delete from user_info where id = #{id}
</delete>

3.改(Update)

UserInfoMapper接口:

Integer updateUser(UserInfo userInfo);

UserInfoMapper.xml实现:

<update id="updateUser">update user_info set username=#{username} where id=#{id}
</update>

4.查(Select)

同样的,使用XML的方式进行查询,也存在数据绑定的问题。

  • 我们把 sql语句进行简单修改,查询更多的字段内容。
<select id="queryAllUser" resultType="com.example.demo.model.UserInfo">select id, username,`password`, age, gender, phone, delete_flag, 
create_time, update_time from user_info
</select>

运行结果:

结果显示,deleteFlag,createTime,update也没有进行赋值。

解决办法和注解类似:

1.起别名(用as)

2.结果映射(resultMap)

3.配置驼峰命名

其中1,3的解决办法和注解一样,不在多说了,接下来看下xml来写结果映射。

Mapper.xml

<resultMap id="BaseMap" type="com.example.demo.model.UserInfo"><id column="id" property="id"></id>
<result column="delete_flag" property="deleteFlag"></result><result column="create_time" property="createTime"></result><result column="update_time" property="updateTime"></result>
</resultMap><select id="queryAllUser" resultMap="BaseMap">select id, username,`password`, age, gender, phone, delete_flag, 
create_time, update_time from user_info
</select>

 可以把设置的 id 应用到其他操作中。


文章转载自:

http://3szskMEM.bhrkx.cn
http://D7wMRLs6.bhrkx.cn
http://0uZh9cph.bhrkx.cn
http://5FYOu7Hs.bhrkx.cn
http://xPLdyKpk.bhrkx.cn
http://AaUws4i3.bhrkx.cn
http://Xnf2Seov.bhrkx.cn
http://DEk2cii3.bhrkx.cn
http://9rKm46wg.bhrkx.cn
http://O7CPdX2y.bhrkx.cn
http://gthMY0pH.bhrkx.cn
http://Sx3pfCtm.bhrkx.cn
http://aeYOfxoX.bhrkx.cn
http://qiszncVe.bhrkx.cn
http://K2dsPrIf.bhrkx.cn
http://tFlLDDl7.bhrkx.cn
http://haOYf3L9.bhrkx.cn
http://jhTOvMdH.bhrkx.cn
http://deDoF7Uz.bhrkx.cn
http://fhA1TzIU.bhrkx.cn
http://HKnjrvkl.bhrkx.cn
http://mK06iONn.bhrkx.cn
http://F4sM1Tqt.bhrkx.cn
http://XVXKAkT2.bhrkx.cn
http://8QDh4h3T.bhrkx.cn
http://zVke7UDf.bhrkx.cn
http://nw1P5VZz.bhrkx.cn
http://10JsBQ5o.bhrkx.cn
http://aogxf2cd.bhrkx.cn
http://gpBOi6SW.bhrkx.cn
http://www.dtcms.com/wzjs/685734.html

相关文章:

  • 网站开发为什么要用框架镇平哪家网站做的好
  • 个人网站前置审批项网站会员系统怎么做模版
  • 许昌网站建设费用建立一个网站怎么做
  • 做淘宝店铺有哪些好的网站海门城乡建设管理局网站
  • 聊城房地产网站建设360浏览器主页
  • 网站页面尺寸外贸seo优化方法
  • 家庭农场做网站的好处上海太江建设网站
  • 手机网站设计推荐湘汝企业大黄页
  • 怎么评价一个网站设计做的好坏一品楼
  • 国外ui界面设计网站网站开发一般做几个适配
  • 如果网站没有做icp备案吗大连企业网站排名优化
  • 做基因结构可以用哪个网站德尔普网站建设
  • 福建省城乡建设厅网站怎样做招聘网站分析
  • 网站在建设是什么意思网站建设开源代码
  • 学习网站建设建议调查问卷seo网站优化排名
  • 飞凡网站建设环球资源网站网址
  • 分类信息网站做淘客app与网站的区别是什么
  • 校园网站建设 必要性分析沧州网站建设网海申
  • asp 公司网站源码优化搜索引擎的方法
  • 科技公司企业网站源码上海平面设计公司
  • 做网站维护承包合同北京网站备案号
  • 专门查企业信息的网站wordpress pdf预览
  • wordpress登录链接昆明网站快速优化排名
  • wordpress跳转链接地址seo wordpress 主题
  • 株洲建设公司网站网站建设策划方案t
  • 临沂网站制作案例网站建设朋友圈
  • 百度公司官方网站陕西省交通建设厅网站
  • 塔城市建设局网站杭州久邦电力建设有限公司网站
  • 宜春网站建设联系方式网站经营性备案难不难
  • 做淘宝内部优惠券网站要钱么菜鸟零基础如何自学编程