Maven项目用jdk动态代理模式向数据库插入数据,显示执行成功但是数据库表中都是null
这是接口中定义的插入方法
int insert(Users users);
这是mapper.xml文件中的insert语句
Users类型已经注册了别名
<insert id="insert" parameterType="users">
insert into t_users(username,birthday,sex,address)
values(username=#{userName},birthday=#{birthday},sex=#{sex},address=#{address})
</insert>
这是实体类的实例变量,构造函数提供了无参,全参,除去id之外4个参数的三种
public class Users {
private Integer id;
private String userName;
private Date birthday;
private String sex;
private String address;
测定方法的代码如下
public class MyTest {
SqlSession sqlSession;
UsersMapper uMapper;
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
@Before
public void openSqlSession() throws IOException {
InputStream in = Resources.getResourceAsStream("SqlMapConfig.xml");
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
sqlSession = factory.openSession();
uMapper = sqlSession.getMapper(UsersMapper.class);
}
@After
public void closeSqlSession(){
sqlSession.close();
}
public void testInsert() throws ParseException {
Users users = new Users("ssss",new Date(),"2","上海市");
Integer num =uMapper.insert(users);
sqlSession.commit();
}
}