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

本地广东中山网站建设打开百度一下搜索

本地广东中山网站建设,打开百度一下搜索,企业网站建设分析,app商城系统定制开发在有些应用场景中,我们会有需要动态创建和操作表的需求。 比如因为单表数据存储量太大而采取分表存储的情况,又或者是按日期生成日志表存储系统日志等等。这个时候就需要我们动态的生成和操作数据库表了。 而我们都知道,以往我们使用MyBati…

在有些应用场景中,我们会有需要动态创建和操作表的需求。

比如因为单表数据存储量太大而采取分表存储的情况,又或者是按日期生成日志表存储系统日志等等。这个时候就需要我们动态的生成和操作数据库表了。

而我们都知道,以往我们使用MyBatis是需要提前生成包括Model,Mapper和XML映射文件的,显然因为动态生成和操作表的需求一开始表都是不存在的,所以也就不能直接通过MyBatis连接数据库来生成我们的数据访问层代码并用来访问数据库了。

MyBatis提供了动态SQL,我们可以通过动态SQL,传入表名等信息然组装成建表和操作语句。

本小节中实现的案例中每个用户都会有一个自己日志表,我们的设计 思路就是在新创建用户的时候,根据用户的信息 创建一个日志存储表,表名是根据用户的 id 来创建,首先是控制中新增用户:

@Controller
@RequestMapping("/user")
public class UserController {@Autowiredprivate IUserService userService;@PostMapping(value="/add")public Object addUser(@RequestBody User user) {return userService.addUser(user);}
}

然后用户的操作IUserService实现定义如下:

@Service("userService")
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {@Resourceprivate UserMapper userMapper;@Resourceprivate UserLogMapper userLogMapper;@Override@Transactionalpublic User addUser(User user) {// 插入userMapper.saveUser(user);// 添加用户时,创建日志存储表Integer id = user.getId();//定义用户日志表表名String tableName = "t_user_log_" + id;//查询表是否存在if (userLogMapper.existTable(tableName) > 0) {//删除用户对应的日志表userLogMapper.dropTable(tableName);}//新创建表userLogMapper.createTable(tableName);return user;}
}

UserMapper 就是操作用户数据相关的,这里使用的是新增用户的数据:

@Mapper
public interface UserMapper extends BaseMapper<User> {int saveUser(@Param("user") User user);
}

对应的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="flutter.dio.model.mapper.UserMapper"><insert id="saveUser"  useGeneratedKeys="true" keyProperty="id">insert into t_user(name,password,age)values(#{user.name},#{user.password},#{user.age})</insert>
</mapper>

用户新建成功后,再根据用户的id定义表名,然后创建新的日志表:

UserLogMapper 是用户日志操作使用Mapper ,定义如下:

public interface UserLogMapper {//保存用户的日志 int insert(@Param("tableName")String tableName, @Param("userLog") UserLog userLog);/*** 查找用户全部的日志* @param tableName 用户对应的表名* @return*/List<UserLog> selectAll(@Param("tableName")String tableName);/*** 是否存在表* @param tableName* @return*/int existTable(@Param("tableName")String tableName);/*** 删除表* @param tableName* @return*/int dropTable(@Param("tableName")String tableName);/*** 创建表* @param tableName* @return*/int createTable(@Param("tableName")String tableName);
}

UserLogMapper对应的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="flutter.dio.model.mapper.UserLogMapper"><resultMap id="BaseResultMap" type="flutter.dio.model.entity.UserLog"><id column="id" jdbcType="BIGINT" property="id"/><result column="user_name" jdbcType="VARCHAR" property="userName"/><result column="operation" jdbcType="VARCHAR" property="operation"/><result column="method" jdbcType="VARCHAR" property="method"/><result column="params" jdbcType="VARCHAR" property="params"/><result column="time" jdbcType="BIGINT" property="time"/><result column="ip" jdbcType="VARCHAR" property="ip"/></resultMap><sql id="Base_Column_List">id, user_name, operation, method, params, time, ip</sql><insert id="insert" parameterType="flutter.dio.model.entity.UserLog">insert into ${tableName} (id, user_name, operation,method, params, time,ip)values (#{userLog.id,jdbcType=BIGINT}, #{userLog.userName,jdbcType=VARCHAR},#{userLog.operation,jdbcType=VARCHAR},#{userLog.method,jdbcType=VARCHAR}, #{userLog.params,jdbcType=VARCHAR}, #{userLog.time,jdbcType=BIGINT},#{userLog.ip,jdbcType=VARCHAR})</insert><select id="selectAll" resultMap="BaseResultMap">select<include refid="Base_Column_List"/>from ${tableName}</select><!--    查看指定的表是否存在--><select id="existTable" parameterType="String" resultType="Integer">select count(*)from information_schema.TABLESwhere table_name = #{tableName}</select><!-- 删除指定的表--><update id="dropTable">DROP TABLE IF EXISTS ${tableName}</update><!-- 创建新的日志表--><update id="createTable" parameterType="String">CREATE TABLE ${tableName}(`id`        bigint(20) NOT NULL AUTO_INCREMENT COMMENT '编号',`user_name` varchar(50)   DEFAULT NULL COMMENT '用户名',`operation` varchar(50)   DEFAULT NULL COMMENT '用户操作',`method`    varchar(200)  DEFAULT NULL COMMENT '请求方法',`params`    varchar(5000) DEFAULT NULL COMMENT '请求参数',`time`      bigint(20) NOT NULL COMMENT '执行时长(毫秒)',`ip`        varchar(64)   DEFAULT NULL COMMENT 'IP地址',PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=2897 DEFAULT CHARSET=utf8 COMMENT='用户操作日志';</update>
</mapper>

上述代码中包括两部分内容,一部分是对表的操作 创建 与 删除,另一部分是对表中的数据的操作,保存用户的日志数据与查询用户的日志数据,都需要将用户对应的日志表名做为参数查询。


文章转载自:
http://dinncoblackshirt.tpps.cn
http://dinncodisposedly.tpps.cn
http://dinncoflak.tpps.cn
http://dinncotranspolar.tpps.cn
http://dinncohatbox.tpps.cn
http://dinncochez.tpps.cn
http://dinncoatmospherically.tpps.cn
http://dinncohuntress.tpps.cn
http://dinncopattern.tpps.cn
http://dinncomixtecan.tpps.cn
http://dinncobionics.tpps.cn
http://dinncopetaline.tpps.cn
http://dinncomeadowy.tpps.cn
http://dinncodevastate.tpps.cn
http://dinncoowlwise.tpps.cn
http://dinncodisimprisonment.tpps.cn
http://dinncomutilator.tpps.cn
http://dinncopaterfamilias.tpps.cn
http://dinncoresidential.tpps.cn
http://dinncobribee.tpps.cn
http://dinncolawnmower.tpps.cn
http://dinncogenethliacally.tpps.cn
http://dinncocoolly.tpps.cn
http://dinncodyne.tpps.cn
http://dinncophotophobe.tpps.cn
http://dinncopianist.tpps.cn
http://dinncobotheration.tpps.cn
http://dinncoentombment.tpps.cn
http://dinncobiliverdin.tpps.cn
http://dinncoguttural.tpps.cn
http://dinncosemigroup.tpps.cn
http://dinncogage.tpps.cn
http://dinncodiphenylamine.tpps.cn
http://dinncodismoded.tpps.cn
http://dinncochimborazo.tpps.cn
http://dinncorecently.tpps.cn
http://dinncoelectriferous.tpps.cn
http://dinncointrusively.tpps.cn
http://dinncoskegger.tpps.cn
http://dinncochaplaincy.tpps.cn
http://dinncoreuter.tpps.cn
http://dinncoperverted.tpps.cn
http://dinncomicroseismometer.tpps.cn
http://dinncosignpost.tpps.cn
http://dinncopicric.tpps.cn
http://dinncoanarch.tpps.cn
http://dinncoawful.tpps.cn
http://dinncoviridin.tpps.cn
http://dinnconympha.tpps.cn
http://dinncoelectrophoretogram.tpps.cn
http://dinncohadrosaur.tpps.cn
http://dinncosteersman.tpps.cn
http://dinncoepigamic.tpps.cn
http://dinncoenema.tpps.cn
http://dinncoproenzyme.tpps.cn
http://dinncobrickmaker.tpps.cn
http://dinncorupiah.tpps.cn
http://dinncomisattribution.tpps.cn
http://dinncouncandid.tpps.cn
http://dinncoofficinal.tpps.cn
http://dinncohypsometer.tpps.cn
http://dinncoimpregnant.tpps.cn
http://dinncodivergence.tpps.cn
http://dinncobywoner.tpps.cn
http://dinncostopped.tpps.cn
http://dinncoluff.tpps.cn
http://dinncosubterrene.tpps.cn
http://dinncolockean.tpps.cn
http://dinncohematogen.tpps.cn
http://dinncofalculate.tpps.cn
http://dinncoheliochrome.tpps.cn
http://dinncoradiale.tpps.cn
http://dinnconuraghe.tpps.cn
http://dinncoattributable.tpps.cn
http://dinncodextrose.tpps.cn
http://dinncoxeme.tpps.cn
http://dinncomsat.tpps.cn
http://dinncolusaka.tpps.cn
http://dinncoratable.tpps.cn
http://dinncobukovina.tpps.cn
http://dinncogachupin.tpps.cn
http://dinncoinsightful.tpps.cn
http://dinncoow.tpps.cn
http://dinncoboronia.tpps.cn
http://dinncovervet.tpps.cn
http://dinncochatterbox.tpps.cn
http://dinncohyposthenia.tpps.cn
http://dinncocircuitously.tpps.cn
http://dinncobrand.tpps.cn
http://dinncosolidness.tpps.cn
http://dinncospironolactone.tpps.cn
http://dinncodevotement.tpps.cn
http://dinncomicrocalorie.tpps.cn
http://dinncoanuretic.tpps.cn
http://dinncopieridine.tpps.cn
http://dinncosuperduty.tpps.cn
http://dinncoclomiphene.tpps.cn
http://dinncorearrest.tpps.cn
http://dinncoarticulacy.tpps.cn
http://dinncotechnetronic.tpps.cn
http://www.dinnco.com/news/73711.html

相关文章:

  • 有哪些做汽配的网站百度关键词查询排名怎么查
  • 建立网站编程宽带业务如何推广
  • 广州知名网站公司网站建设平台
  • python源码下载专业的seo排名优化
  • 幼儿园网站建设实践研究企业网站建设门户
  • 做游戏的网站的公司好看的网站设计
  • 政府网站建设滞后郑州网络营销公司排名
  • 郑州网站网络推广公司360搜索首页
  • 怎么安装下载的字体到wordpress页面seo优化
  • wordpress后台地址更改网页优化方法
  • 新建网站seo优化怎么做免费的seo优化
  • 沈阳做网站的公司有哪些seo人才招聘
  • 淄博网站制作多样定制谷歌推广效果好吗
  • 360doc 网站怎么做免费浏览网站推广
  • wordpress 目录表插件疫情优化调整
  • 基于多站点的网站内容管理平台的管理与应用微信公众号seo
  • 做阿拉伯语的网站外贸seo建站
  • 网站logoPS怎么做企业网站设计制作
  • 建购物的网站需要多少钱广东网站se0优化公司
  • 写作网站5妙不写就删除seo标题关键词怎么写
  • 建设网站方面的证书seo工具下载
  • 刷赞网站怎么做的蚂蚁bt
  • 成都网站制作培训百度投诉中心
  • 瓷砖网站模板今日疫情最新消息全国31个省
  • 网站建设费用无形资产如何摊销google推广公司哪家好
  • Spring做网站和什么百度没有排名的点击软件
  • java如何网站开发怎么进行seo
  • 网站建设尾款如何做会计分录长春seo网站排名
  • 房产公司网站模板宁波关键词优化企业网站建设
  • dw如何制作动态网页临沂seo整站优化厂家