UserInfoMapper u.*,r.id as "rinfo.id",r.name as "rinfo.name" select * from userinfo; select * from userinfo where id=#{id} select * from userinfo where name=#{name} and pwd=#{pwd} select seq2.nextval from dual insert into userinfo values#{i
testu.*,r.id as "rinfo.id",r.name as "rinfo.name" select seq2.nextval from dual insert into userinfo values#{id},#{name},#{pwd},#{roleid}update userinfo where id=0 name=#{name}, pwd=#{pwd}, roleid=#{roleid}
package pidan.test; import java.io.IOException; import java.io.InputStream; 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 pidan.dao.UserInfoDao; public class Test { static SqlSessionFactory sqlSessionFactory; public static void main(String[] args) throws Exception { SqlSession session1=getSqlSession(); //二级缓存 UserInfoDao dao1=session1.getMapper(UserInfoDao.class); System.out.println(dao1.getUserById(1005)); Thread.sleep(15000); session1.close(); SqlSession session2=getSqlSession(); UserInfoDao dao2=session2.getMapper(UserInfoDao.class); System.out.println(dao2.getUserById(1005)); } static{ try { String resource="mybatis.xml"; InputStream inputStream=Resources.getResourceAsStream(resource); sqlSessionFactory =new SqlSessionFactoryBuilder().build(inputStream); } catch (IOException e) { e.printStackTrace(); } } //SqlSession不能使用单例,sqlSessionFactory可以使用单例 public static SqlSession getSqlSession() { return sqlSessionFactory.openSession(); } }