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));
}