日期:2014-05-18  浏览次数:20577 次

jdbcTemplate问题

@Override
public int save(final TestEntity testEntity){
KeyHolder keyHolder = new GeneratedKeyHolder();
this.getJdbcTemplate().update(new PreparedStatementCreator(){
            @Override
            public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
             //写sql语句
             String sql = "insert into testEntity(name,passwd) values(?,?)";
             //设置PreparedStatement并设置它要返回主键
                PreparedStatement ps = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
                //设置要插入的值
                ps.setString(1, testEntity.getName());
                ps.setString(2, testEntity.getPasswd());
                return ps;
            }  
        }, keyHolder);
//返回主键
return keyHolder.getKey().intValue();
}

上面是刚学和jdbcTemplate保存对象返回主键的方法,可是我保存的时候的确返回了主键,但是没有保存对象 是什么情况?数据库里面没有任何数据,倒是主键在涨……

private static TestEntityService testEntityService;

@BeforeClass
public static void setUpBeforeClass() throws Exception {
ApplicationContext ctx = new FileSystemXmlApplicationContext("src/applicationContext.xml");
testEntityService = (TestEntityService)ctx.getBean("TestEntityService");
}

@Before
public void setUp() throws Exception {
}

@Test
public void saveTest(){
TestEntity test = new TestEntity();
test.setName("abc");
test.setPasswd(MD5Util.md5("123"));
System.out.println(testEntityService.save(test));
}
结果是能输出主键,但是数据库里面没有数据?
jdbcTemplate

------解决方案--------------------
莫非这个与事务有关?
------解决方案--------------------<