使用Mybatis配置数据库,向里面存数据存不进去(存数据不会报错,但就是在数据库中没有添加数据),而取数据可以取到但是不能完全匹配,如下:
明明有user_name等,但是结果中显示NULL
User{id='12', email='12', userName='null', passWord='null', state=12, gender='12', telephone='12', birthday=null, joinDate=null, deptId='null', dept=null}
我在同项目下还有两个其他表的访问都是没问题的,所以整体的配置是没有问题的,应该就是这一张表的问题。
可能是映射配置文件的问题,映射部分如下:
<resultMap id="user" type="cn.luoxin88.domain.system.User">
<id column="id" property="id"></id>
<result column="email" property="email"></result>
<result column="user_name" property="userName"></result>
<result column="pass_word" property="passWord"></result>
<result column="state" property="state"></result>
<result column="gender" property="gender"></result>
<result column="telephone" property="telephone"></result>
<result column="birthday" property="birthday"></result>
<result column="join_date" property="joinDate"></result>
<result column="dept_id" property="deptId"></result>
<association
property="dept"
column="dept_id"
javaType="cn.luoxin88.domain.system.Dept"
select="cn.luoxin88.dao.system.DeptDao.findById"
/>
</resultMap>
<!--实现相关数据查询的功能-->
<insert id="save" parameterType="cn.luoxin88.domain.system.User">
insert into users
values(#{id},#{email},#{userName},#{passWord},#{state},#{gender},#{telephone},#{birthday},#{joinDate},#{deptId});
</insert>
<select id="findById" parameterType="java.lang.String" resultType="user">
select id,email,user_name,pass_word,state,gender,telephone,birthday,join_date,dept_id from users where id=#{id};
</select>
bean:
private String id;
private String email;
private String userName;
private String passWord;
private Long state;
private String gender;
private String telephone;
private Date birthday;
private Date joinDate;
private String deptId;
private Dept dept;
数据表:
mapper文件的select的resultType改为resultMap="user",然后直接select * 就可以了
中间加一句 jdbcTYpe
你DEBUG一下 把执行的SQL打印出来 不然不太好分析原因
save方法保存不成功的原因可能是你的传值和数据库的字段类型不匹配,对于null值的尽量先添加一个默认值,不使用null调用save方法试试,同时debug模式跟踪下,并观察控制台的sql语句,单独放到客户端执行看看是否成功