mysql-数据类型和java数据类型对应关系

1,736 阅读4分钟

常用数据类型

数字

0.数字-大
BIGINT(20) //海量数据的id字段。天文数字。
1.数字-中
Long int(11) //id字段,值是数字的字段。
int就是integer的缩写,两种写法都可以。
2.数字-小
Integer tinyint(1) //状态字段

总结
小 //最大1个字节,8位。最佳实践是1位
中 //最大4个字节,32位。最佳实践是11位
大 //最大8个字节,64位。最佳实践是20位

字符串

3.字符和字符串
String varchar(45) //字符串
char(1) //单个字符

注:一个是定长,一个是边长。

日期

4.时间
sql.Timestamp timestamp //时间戳类型,即年月日 时分秒格式


说明
左边的是java数据类型,右边的是mysql数据类型。


代码

CREATE TABLE `employee` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `employee_num` varchar(45) DEFAULT NULL COMMENT '员工编号。规则是分公司名字缩写+数字(例如,WZSZ001)。',
  `name` varchar(45) DEFAULT NULL,
  `mobile_phone` varchar(45) DEFAULT NULL,
  `department_id` int(11) DEFAULT NULL,
  `work_city_id` int(11) DEFAULT NULL COMMENT '工作地点所在城市',
  `company_id` int(11) DEFAULT NULL COMMENT '分公司',
  `work_status` tinyint(1) DEFAULT NULL COMMENT '在职状态:在职1,离职0',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8
CREATE TABLE `department` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(45) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL COMMENT '部门名字',
  `parent_id` int(11) DEFAULT NULL,
  `leader` int(11) DEFAULT NULL COMMENT '部门负责人。值来自employee的id。',
  `create_time` datetime DEFAULT NULL COMMENT '创建时间',
  `update_time` datetime DEFAULT NULL COMMENT '更新时间',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=17 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci

长度M

1.定义
数据类型(长度M)

2.长度M
作用?一般没什么作用,因为数据类型一般都是占固定字节数量。之所以显式写出来,只是为了一眼看上去知道数据的长度大小。

mybatis-输入和输出

1.增
输入 实体类
输出 int类型,但不需要显式配置 //int 成功1,失败其他

2.删
输入 id字段
输出 int类型,但不需要显式配置 //int 成功1,失败其他

3.查
输入 id/实体类/多个查询参数
输出 实体类或map //显式配置resultType="实体类名字"或resultMap="映射id"

4.改
输入 实体类/修改字段的值
输出 int类型,但不需要显式配置 //int 成功1,失败其他


代码

<?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.wz.bpm.mapper.DepartmentMapper">
<!-- 查询结果 -->
	<resultMap id="DepartmentResultMap"
		type="com.wz.bpm.bean.Department">
		<id property="id" column="id" />
		<result property="name" column="name" />
		<result property="parentId" column="parent_id" />
		<result property="leader" column="leader" />
		<collection property="children"
			ofType="com.wz.bpm.bean.Department"
			select="com.wz.bpm.mapper.DepartmentMapper.getDepByPid" column="id">
		</collection>
	</resultMap>
	
	<resultMap id="DepartmentResultMap2"
		type="com.wz.bpm.bean.Department">
		<id property="id" column="id" />
		<result property="name" column="name" />
		<result property="parentId" column="parent_id" />
		<result property="leader" column="leader" />
		<result property="createTime" column="create_time" />
	</resultMap>
	
<!-- 查询所有部门(包含父子关系) -->
	<select id="getDepByPid" resultMap="DepartmentResultMap">
		select d.*
		from department d
		where d.parent_id=#{pid}
	</select>
	
	<!-- 添加部门 -->

	<insert id="addDepartment" parameterType="com.wz.bpm.bean.Department">

		INSERT INTO department(name,parent_id,create_time,leader) 

		VALUES(#{name},#{parentId},#{createTime,jdbcType=TIMESTAMP},#{leader})

	</insert>
	
	<!-- 删除部门 -->
	 <delete id="deleteDepartment" parameterType="Long">
        DELETE 
        FROM department
         WHERE id=#{id}
    </delete>
    
    <!-- 查看部门 -->
    <select id="getDepartment" resultMap="DepartmentResultMap2">
      select *
      from department
      where id=#{id}
    </select>
    
    <update id="updateDepartment">
        update department
        <set>
            <if test="name != null">
                name = #{name},
            </if>
            <if test="parentId != null">
                parent_id = #{parentId},
            </if>
            <if test="leader != null">
                leader = #{leader},
            </if>
            <if test="updateTime != null">
                update_time = #{updateTime},
            </if>
        </set>
        where id = #{id}
    </update>
	

<!-- 查询所有部门 -->
	<select id="getAllDeps" resultType="com.wz.bpm.bean.Department">
		select d.*,c.name as company_name
		from department d,company c
		where d.company_id and c.company_id
	</select>
</mapper>

java-日期数据类型

1.util.Date

2.sql.Date
Timestamp //util包没有Timestamp,sql包都是对util.Date的简单封装。

但是本质都是基于毫秒,来得到时间或当前时间。


一、与时间日期有关的类。

  java.util.Date。实现类,其对象具有时间、日期组件。

  java.util.Calendar。抽象类,其对象具有时间、日期组件。

  java.sql.Date。实现类,其对象具有日期组件。

  java.sql.Time。实现类,其对象具有时间组件。

  java.sql.Timestamp。实现类,其对象具有时间日期组件。

  java.text.DateFormat。抽象类,其对象格式化时间日期。


版权声明:本文为CSDN博主「kevin_vv」的原创文章,遵循CC 4.0 by-sa版权协议,转载请附上原文出处链接及本声明。 原文链接:blog.csdn.net/u013758116/…

参考

www.runoob.com/mysql/mysql…

www.cnblogs.com/xrq730/p/84…

www.cnblogs.com/zbseoag/arc…

www.kancloud.cn/thinkphp/my…

www.kancloud.cn/thinkphp/my…

github.com/jaywcjlove/…