在使用mybatis根据ID自增长插入语句问题?
在mybatis核心配置文件中解决
MYSQL解决办法:
<!-- public void addDept(Dept dept);
mysql支持自增主键;自增主键的获取,mybatis也是利用statement.getGeneratedKeys()
要想前台获取添加的数字,添加两个属性
useGeneratedKeys="true": 使用自增主键获取主键值
keyProperty: 对应的主键属性,也就是mybatis获取到主键值以后,将这个值封装给javaBean的哪个属性
-->
<insert id="addDept" useGeneratedKeys="true" keyProperty="deptno" databaseId="mysql" >
insert into dept(dname,loc) values (#{dname},#{loc});
</insert>
Oracle解决办法:
<!--Oracle不支持 自增;Oracle使用序列序列来模拟自增;
每次插入的数据的主键是从序列中拿到的值:如歌获取到这个值?
-->
<insert id="addDept" databaseId="oracle" >
<!-- keyProperty:查出的主键封装给JavaBean的哪个属性
order="BEFORE":当前SQL在插入之前运行
AFTER:当前SQL在插入之后运行
resultType: 查出数据的返回值类型
BEFORE运行顺序:
先运行selectKey查询id的sql;查出id的值封装给javaBean的id属性
再运行插入的SQL;就可以取出id属性对应的值
AFTER:
先 运行插入的SQL(从序列中取出新值作为id)
再运行selectKey查询id的sql
-->
<selectKey keyProperty="deptno" order="BEFORE" resultType="Integer">
<!-- 编写查询主键的SQL语句 -->
<!-- BEFORE:-->
SELECT seq_deptno.nextval FROM dual
<!-- AFTER:
SELECT seq_deptno.currval FROM dual
-->
</selectKey>
<!-- 插入时的主键是从序列中拿到的 -->
<!-- BEFORE: -->
insert into dept(deptno,dname,loc)
values (#{deptno},#{dname},#{loc})
<!-- AFTER:
insert into dept(deptno,dname,loc)
values (seq_deptno.nextval,#{dname},#{loc})
-->
</insert>
---------------------
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%")来设置条件;
---------------------