mybatis 更新数据表、自动增长序列、
一、更新数据表
1.在映射文件中添加:
<update id="updateStudentById" parameterType="Student">
update student set name=#{name},age=#{age} where id=#{id}
</update>
2.在测试文件中添加(先查询出修改的对象,然后在修改)
String statement="com.huayu.mapper.studentMapper"+".selectStudentById";
// String statement1="com.huayu.mapper.studentMapper"+".updateStudentById";
// Student st=sqlSession.selectOne (statement,6);
// st.setName("huahua");
// st.setAge(20);
// sqlSession.update(statement1, st);
二、添加数据时,针对id设主键,避免唯一约束,如何实现自动增长
1.第一种方式:
在oracle中.
a、添加序列表
b、在映射文件中找到insert元素输入以下内容,如:
<insert id="insertStudentById" parameterType="Student">
<selectKey keyProperty="id" resultType="int" order="BEFORE">
select s_t_seq.nextval from dual
</selectKey>
insert into student values(#{id},#{name},#{age})
</insert>
在触发器中
2.第二种方式
在mysql中.
1.打开cmd输入mysql -u root
2.创建数据库名称:create database 名字;
3.use 名字
4.create table Student(
student_id int(6) not null primary key auto_increment,
student_name varchar(10) not null,
student_age int(2) not null
);
5.将映射文件insert修改为
<insert id="insertStudent" parameterType="Student" useGenerateKeys="true"
keuProperty="id">
insert 语句
</insert>
6、将driver改成com.mysql.jdbc.Driver
将url改成jdbc:mysql://localhost:3306/mysql数据库名字
三、
1.mybatis嵌套结果(使用association进行关联)
首先,在映射文件中根据班级id查询班级的信息
<select id="selectClassById" parameterType="int" resultMap="selectClasses">
select * from class c,teacher t where c.t_id=t.t_id and c_id = #{id}
</select>
使用resultMap引用对象的属性,注意id和上面的resultMap名称一致
<resultMap type="Classes" id="selectClasses">
<id property="id" column="c_id"/>
<result property="name" column="c_name"/>
<关联,一对一映射>
<association property="teacher" javaType="Teacher">
<id property="id" column="t_id"/>
<result property="name" column="t_name"/>
</association>
</resultMap>
2.嵌套查询
通过多条select语句得到结果
先根据班级id查出班级信息
<select id="selectClass1" parameterType="int" resultMap="selectClassMap">
select * from class where c_id=#{id}
</select>
再根据resultMap去引用resultMap标签里的属性,它包括:
Classes类的属性
<resultMap type="Classes" id="selectClassMap">
<id property="id" column="c_id"/>
<result property="name" column="c_name"/>
和Classes关联的teacher类的属性
<association property="teacher" column="t_id" select="selectTeacher">
<id property="id" column="t_id"/>
<result property="name" column="t_name"/>
</association>
</resultMap>
四、一对多映射
五、动态sql;
1.先把要使用的条件写成java类的属性;
2.在映射文件中查询语句;
<select id="selectTt" parameterType="Tt" resultType="Student">
select * from student where age between #{minAge} and #{maxAge}
//判断模糊条件是否为空
<if test='pipi!="%null%"'>
and name like #{pipi}
</if>
在测试类里面也要写成t.setPipi("%h%")来设置条件;
一、更新数据表
1.在映射文件中添加:
<update id="updateStudentById" parameterType="Student">
update student set name=#{name},age=#{age} where id=#{id}
</update>
2.在测试文件中添加(先查询出修改的对象,然后在修改)
String statement="com.huayu.mapper.studentMapper"+".selectStudentById";
// String statement1="com.huayu.mapper.studentMapper"+".updateStudentById";
// Student st=sqlSession.selectOne (statement,6);
// st.setName("huahua");
// st.setAge(20);
// sqlSession.update(statement1, st);
二、添加数据时,针对id设主键,避免唯一约束,如何实现自动增长
1.第一种方式:
在oracle中.
a、添加序列表
b、在映射文件中找到insert元素输入以下内容,如:
<insert id="insertStudentById" parameterType="Student">
<selectKey keyProperty="id" resultType="int" order="BEFORE">
select s_t_seq.nextval from dual
</selectKey>
insert into student values(#{id},#{name},#{age})
</insert>
在触发器中
2.第二种方式
在mysql中.
1.打开cmd输入mysql -u root
2.创建数据库名称:create database 名字;
3.use 名字
4.create table Student(
student_id int(6) not null primary key auto_increment,
student_name varchar(10) not null,
student_age int(2) not null
);
5.将映射文件insert修改为
<insert id="insertStudent" parameterType="Student" useGenerateKeys="true"
keuProperty="id">
insert 语句
</insert>
6、将driver改成com.mysql.jdbc.Driver
将url改成jdbc:mysql://localhost:3306/mysql数据库名字
三、
1.mybatis嵌套结果(使用association进行关联)
首先,在映射文件中根据班级id查询班级的信息
<select id="selectClassById" parameterType="int" resultMap="selectClasses">
select * from class c,teacher t where c.t_id=t.t_id and c_id = #{id}
</select>
使用resultMap引用对象的属性,注意id和上面的resultMap名称一致
<resultMap type="Classes" id="selectClasses">
<id property="id" column="c_id"/>
<result property="name" column="c_name"/>
<关联,一对一映射>
<association property="teacher" javaType="Teacher">
<id property="id" column="t_id"/>
<result property="name" column="t_name"/>
</association>
</resultMap>
2.嵌套查询
通过多条select语句得到结果
先根据班级id查出班级信息
<select id="selectClass1" parameterType="int" resultMap="selectClassMap">
select * from class where c_id=#{id}
</select>
再根据resultMap去引用resultMap标签里的属性,它包括:
Classes类的属性
<resultMap type="Classes" id="selectClassMap">
<id property="id" column="c_id"/>
<result property="name" column="c_name"/>
和Classes关联的teacher类的属性
<association property="teacher" column="t_id" select="selectTeacher">
<id property="id" column="t_id"/>
<result property="name" column="t_name"/>
</association>
</resultMap>
四、一对多映射
五、动态sql;
1.先把要使用的条件写成java类的属性;
2.在映射文件中查询语句;
<select id="selectTt" parameterType="Tt" resultType="Student">
select * from student where age between #{minAge} and #{maxAge}
//判断模糊条件是否为空
<if test='pipi!="%null%"'>
and name like #{pipi}
</if>
在测试类里面也要写成t.setPipi("%h%")来设置条件;