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

东莞网站制作外包湖南郴州建设局网站

东莞网站制作外包,湖南郴州建设局网站,电商网站建设要多少钱,影视网站怎么做目录👑 配置 写持久层代码 mapper接口 数据持久层的实现,.xml的形式。 select 字段映射 MyBatis配置驼峰命名⭐ 起别名 结果映射 含参数查询 insert delete update 配置 需要配置MySQL驱动类、登录名、密码、数据库连接字符串。 这里是在a…

目录👑

配置

写持久层代码

mapper接口

数据持久层的实现,.xml的形式。

select

字段映射

MyBatis配置驼峰命名⭐

起别名

结果映射

含参数查询

insert

delete

update


配置

需要配置MySQL驱动类、登录名、密码、数据库连接字符串。

这里是在application.yml中配置

spring:datasource:url: jdbc:mysql://127.0.0.1:3306/mybatis_test?characterEncoding=utf8&useSSL=false&allowPublicKeyRetrieval=trueusername: rootpassword: **** # 如果密码password是纯数字的,需要加上" "否则会报错。driver-class-name: com.mysql.cj.jdbc.Drivermybatis:configuration:log-impl: org.apache.ibatis.logging.stdout.StdOutImpl # 配置打印 MyBatis⽇志map-underscore-to-camel-case: true #配置驼峰⾃动转换mapper-locations: classpath:mapper/*.xml 

 mapper-locations: classpath:mapper/*.xml 
  这个是设置创建mybatis XML文件的路径, 在resources下的mapper的所有.xml文件

写持久层代码

mapper接口

import com.bit.mybatis.model.UserInfo;
import org.apache.ibatis.annotations.Mapper;import java.util.List;
@Mapper
public interface UserInfoXmlMapper {List<UserInfo> selectAll();//alt+enter
}

数据持久层的实现,.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.bit.mybatis.mapper.UserInfoXmlMapper"> <!-- 路径+类名,表示要实现的是谁--></mapper>

namespace的内容要和实现的mapper所在路径+接口名。

不一样可能就会出现绑定异常

MyBatisX插件可以将对应的xml和mapper接口关联起来,

点击小鸟就能互相跳转。

select

查询所有用户的具体实现

<?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.bit.mybatis.mapper.UserInfoXmlMapper"> <!-- 路径+类名,表示要实现的是谁--><select id="selectAll" resultType="com.bit.mybatis.model.UserInfo">select * from user_info
-- select id, username, password, age, gender, phone, delete_flag as deleteFlag ,create_time as createTime, update_time as updateTime from user_info;</select>
</mapper>

这里用到的数据库表和实体类都和上篇文章中用到的一样。

我们下载了MyBatisX插件后,在mapper接口的方法上按alt+enter,就会在对应xml的<mapper></mapper>标签之中生成

<select id="selectAll" resultType="com.bit.mybatis.model.UserInfo"></select>

select表示用来执行数据库查询操作,id对应接口中具体的方法,resultType对应返回的数据类型 也就是实体类的路径。

我们可以直接在select标签中写sql语句。

字段映射

MyBatis配置驼峰命名⭐
mybatis:configuration: log-impl: org.apache.ibatis.logging.stdout.StdOutImpl # 配置打印 MyBatis⽇志map-underscore-to-camel-case: true #配置驼峰⾃动转换

这和MyBatis注解开发中一样,配置了之后,mybatis会自动将数据库表中含下划线的字段名转换成小驼峰的形式,这样就能映射成功。

这是最推荐的方式。

起别名

在写sql语句时给含下划线的字段取一个别名

  <select id="selectAll" resultType="com.bit.mybatis.model.UserInfo">
--         select * from user_infoselect id, username, password, age, gender, phone,delete_flag as deleteFlag ,
create_time as createTime, 
update_time as updateTime 
from user_info;</select>

结果映射

<?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.bit.mybatis.mapper.UserInfoXmlMapper"> <!-- 路径+类名,表示要实现的是谁--><resultMap id="BaseMap" type="com.bit.mybatis.model.UserInfo">--><result column="delete_flag" property="deleteFlag"></result><result column="create_time" property="createTime"></result><result column="update_time" property="updateTime"></result></resultMap><select id="selectAll" resultMap="BaseMap">select * from user_info</select> </mapper>

<resultMap>里column表示表中的字段,property中表示字段对应的名字。

select标签中resultMap的内容和要对应的resultMap中的id一样。

resultMap中的type对应返回的数据类型,也就是实体类的路径。

含参数查询

import com.bit.mybatis.model.UserInfo;
import org.apache.ibatis.annotations.Mapper;import java.util.List;
@Mapper
public interface UserInfoXmlMapper {UserInfo selectById(Integer id);
}
  <select id="selectById" resultType="com.bit.mybatis.model.UserInfo">select * from user_info where id = #{id};</select>
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;import static org.junit.jupiter.api.Assertions.*;@SpringBootTest
class UserInfoXmlMapperTest {@Autowiredprivate UserInfoXmlMapper userInfoXmlMapper;@Testvoid selectById() {System.out.println(userInfoXmlMapper.selectById(1));}
}

insert

import com.bit.mybatis.model.UserInfo;
import org.apache.ibatis.annotations.Mapper;import java.util.List;
@Mapper
public interface UserInfoXmlMapper {Integer insertUser(UserInfo userInfo);
}

如果insertUser的参数用@param重命名,比如insertUser(@Param("user")UserInfo userInfo),

sql语句中values后面就是(#{user.username},#{user.password},#{user.age})

<insert id="insertUser">insert into user_info(username, password,age) values (#{username},#{password},#{age})</insert>

测试

package com.bit.mybatis.mapper;import com.bit.mybatis.model.UserInfo;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;import static org.junit.jupiter.api.Assertions.*;@SpringBootTest
class UserInfoXmlMapperTest {@Autowiredprivate UserInfoXmlMapper userInfoXmlMapper;@Testvoid insertUser() {UserInfo userInfo = new UserInfo();userInfo.setAge(99);userInfo.setUsername("kdfj");userInfo.setPassword("809889");Integer result =  userInfoXmlMapper.insertUser(userInfo);System.out.println("受影响的条数:" + result);}
}

insert在xml中和用注释一样还是能获取到递增主键的值。

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

在insert中添加useGeneratedKeys="true" keyProperty="id",

@Test
void insertUser() {UserInfo userInfo = new UserInfo();userInfo.setAge(35);userInfo.setUsername("喜羊羊");userInfo.setPassword("eefd9");Integer result =  userInfoXmlMapper.insertUser(userInfo);System.out.println("受影响的条数:" + result + ",id:" + userInfo.getId());
}

尽管userInfo没有通过set方法来设置id,还是可以获取到id的值。

delete

import com.bit.mybatis.model.UserInfo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;import java.util.List;
@Mapper
public interface UserInfoXmlMapper {Integer deleteById(Integer id);
}
<?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.bit.mybatis.mapper.UserInfoXmlMapper"><!-- 路径+类名,表示要实现的是谁--><delete id="deleteById">delete from user_info where id = #{id}</delete>
</mapper>

测试

package com.bit.mybatis.mapper;import com.bit.mybatis.model.UserInfo;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;import static org.junit.jupiter.api.Assertions.*;@SpringBootTest
class UserInfoXmlMapperTest {@Autowiredprivate UserInfoXmlMapper userInfoXmlMapper;@Testvoid deleteById() {Integer result =  userInfoXmlMapper.deleteById(2);System.out.println("受影响的条数:" + result);}
}

update

import com.bit.mybatis.model.UserInfo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;import java.util.List;
@Mapper
public interface UserInfoXmlMapper {Integer updateById(String username,Integer id);
}
<update id="updateById">update user_info set username=#{username} where id = #{id}</update>
    @Testvoid updateById() {Integer result =  userInfoXmlMapper.updateById("小美",8);System.out.println("受影响的条数:" + result);

http://www.dtcms.com/a/434088.html

相关文章:

  • 个人网站项目策划书seo相关ppt
  • 网站制作哪个公司好湖南云网站建设
  • 网站建设公司 广告法被处罚移动app开发技术
  • 二级网站都在一台服务器怎么做域名用了wordpress的网站
  • 先进的网站开发技术海南省建设集团有限公司网站
  • 网线制作考核标准sem和seo的区别
  • 网站流量是如何计算的网站页面字体设置
  • 做城通网盘资源网站的源码品牌建设 高质量发展
  • 秦州区建设局网站自己建网站 wordpress
  • 怎么用网站源码建站代刷网站是怎么做的
  • 如何做一个导航网站做网络推广可以通过哪些渠道推广
  • 常德地区网站建设wordpress云主机模板
  • 如何做国外的电商网站珠海网站建设优化
  • 如何做好一个企业网站设计给宝宝做辅食的网站
  • ftp怎么上传网站做网站和服务器的大小有关吗
  • 绍兴市建设银行网站物流管理系统
  • 网站建设采购公告模板网站 动易
  • 单页面网站如何seo网站上加一个浮动小框怎么做
  • 广东模板网站建设报价湛江网络营销
  • a做爰视频免费网站广州网站设计有哪些专业
  • 网站申请免费山东做网站的
  • 台州网站制作维护最火的电商平台
  • 徐州企业自助建站做网站还要数据库吗
  • 如何选择坪山网站建设长春做网站建设的公司
  • 网站建设包含美工昆明百度seo
  • 网站空间多大云阳一平米网站建设
  • 云浮东莞网站建设合肥专业做网站的公司
  • 买了域名如何建立网站室内设计师在哪里找
  • 怎么做捕鱼网站广州市新闻发布会
  • 网站用开源cms做网站需要什么认证