用myBatis插入数据时,程序可以正常运行,但是数据却没有添加到数据库中
packagecom.mybatis.demo;importjava.io.Reader;publicclassTest{privatestaticSqlSessionF...
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> 展开
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> 展开
3个回答
展开全部
getSqlMapClientTemplate().insert("Users.addUser",user);增加操作都没有
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2017-12-15
展开全部
记得执行 sqlSession.commit();
然后关闭sqlSession;
然后关闭sqlSession;
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2014-04-01
展开全部
你哪执行保存了?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询