mybatis获取自增长id insert into bill (storeId, no, menthod, amount, type, account, orderNo, description, createTime ) values ( ${storeId}, #{no}, ${menthod}, #{amount}, #{type}, ${account}, #{orderNo}, #{description}, now() ) select *
package mybatis; import java.io.IOException; import java.io.Reader; import java.util.HashMap; import java.util.Map; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import org.junit.Before; import org.junit.Test; public class BillService { SqlSession sqlSession = null; @Before public void init() throws IOException{ /** * 初始化SqlSession */ Reader reader = Resources.getResourceAsReader("mybatis.xml"); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader); sqlSession = sqlSessionFactory.openSession(true); } @Test public void m1(){ Map insert into bill (storeId, no, menthod, amount, type, account, orderNo, description, createTime ) values ( ${storeId}, #{no}, ${menthod}, #{amount}, #{type}, ${account}, #{orderNo}, #{description}, now() ) param = new HashMap (); param.put("storeId", 1); param.put("no", "123456789"); param.put("menthod", 1); param.put("amount", 12333); param.put("type", 1); param.put("account", 123); param.put("orderNo", "45678"); param.put("description", "description"); int x =sqlSession.insert("bill.addBill",param); sqlSession.commit();//重要,一定要提交事务 sqlSession.selectList("bill.getBill"); System.out.println(x); System.out.println("id:"+param.get("id")); } }