用myBatis插入数据时,程序可以正常运行,但是数据却没有添加到数据库中
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用myBatis插入数据时,程序可以正常运行,但是数据却没有添加到数据库中相关的知识,希望对你有一定的参考价值。
package com.mybatis.demo;
import java.io.Reader;
public class Test
private static SqlSessionFactory sqlSessionFactory;
private static Reader reader;
static
try
reader = Resources.getResourceAsReader("Configuration.xml");
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
catch(Exception e)
e.printStackTrace();
public static SqlSessionFactory getSession()
return sqlSessionFactory;
//添加用户
public void addUser()
User user = new User();
user.setId(4);
user.setUserAddress("人民广场");
user.setUserName("Birds");
user.setUserAge("102");
SqlSession session = sqlSessionFactory.openSession();
try
IUserOperation userOperation = session.getMapper(IUserOperation.class);
session.commit();
System.out.println("当前增加的用户id为:"+user.getId());
finally
session.close();
public static void main(String[] args)
Test testUser = new Test();
testUser.addUser();
<?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.mybatis.inter.IUserOperation" >
<select id="selectUserByID" parameterType="int" resultType="User" >
select * from user where id=#id
</select>
<insert id="addUser" parameterType="User"
useGeneratedKeys="true" keyProperty="id">
insert into user(id,userName,userAge,userAddress) values(#id,#userName,#userAge,#userAddress)
</insert>
</mapper>
然后关闭sqlSession; 参考技术C 你哪执行保存了?
以上是关于用myBatis插入数据时,程序可以正常运行,但是数据却没有添加到数据库中的主要内容,如果未能解决你的问题,请参考以下文章