test1 public static void main(String[] args) throws Exception{String resource="mybatis.xml";InputStream inputStream=Resources.getResourceAsStream(resource);SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream
public static void main(String[] args) throws Exception{ String resource="mybatis.xml"; InputStream inputStream=Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream); SqlSession session=sqlSessionFactory.openSession(); System.out.println(session); UserInfo info=session.selectOne("user.getUsersById",101); info.setPwd("a"); session.update("user.update",info); System.out.println(info); UserInfo info2=new UserInfo(105,"abc","123",2); session.insert("user.add",info2); session.update("user.update",info); session.delete("user.deleteById",105); // Mapmapper.xmlmap=new HashMap (); // map.put("id", 104); // map.put("name", "aaa"); // map.put("pwd", "123"); // map.put("rolesid", 2); //// session.insert("user.add",map); // System.out.print(session.insert("user.selectBySql",map)); System.out.println(session.selectOne("user.getUsersById",104)); // Map map=new HashMap (); map.put("sql", "select * from userinfo"); List list=session.selectList("user.selectBySql", map); for (UserInfo userInfo : list) { System.out.println(userInfo); } session.commit(); session.close(); } }
增加dao层,编写dao层接口,利用mapper实现insert into userinfo values(#{id},#{name},#{pwd},#{rolesid}) delete userinfo where id=#{id} update userinfo set name=#{name},pwd=#{pwd},rolesid=#{rolesid} where id=#{id}
package dao; import java.util.List; import org.apache.ibatis.annotations.Param; import bean.UserInfo; public interface UserInfoDao { List日志(方便调试和纠错)getUserAll(); List getUserById(int id); int add(UserInfo info); int deleteById(int id); List getUserByNameAndPwd(@Param("name") String name,@Param("pwd") String pwd); }
log4j.rootLogger=error,Console log4j.appender.Console=org.apache.log4j.ConsoleAppender log4j.appender.Console.layout=org.apache.log4j.PatternLayout log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%ntest2
public static void main(String[] args) { SqlSession session1=getSqlSession(); UserInfoDao dao1=session1.getMapper(UserInfoDao.class); System.out.println(dao1.getUserById(101)); // List动态添加sql语句进行多表联查list=session1.getMapper(UserInfoDao.class).getUserAll(); List list=dao1.getUserAll(); System.out.println(list); UserInfo info = new UserInfo(105,"abd","123",2); System.out.println(dao1.add(info)); Map map=new HashMap (); map.put("id", 105); map.put("name", "ooo"); map.put("pwd", "123"); map.put("rolesid", 3); System.out.println(session1.insert("dao.UserInfoDao.add", map)); dao1.deleteById(105); // session1.commit(); session1.close(); } static{ try { String resource="mybatis.xml"; InputStream inputstream =Resources.getResourceAsStream(resource); sqlSessionFactory =new SqlSessionFactoryBuilder().build(inputstream); } catch (Exception e) { e.printStackTrace(); } } //SqlSession不可以使用单例,SqlSessionFactory可以使用 public static SqlSession getSqlSession(){ return sqlSessionFactory.openSession(); } }
u.*,r.id as "rinfo.id",r.name as "rinfo.name" update userinfo dao.update(new UserInfo(0, "a", "b", null,null)); SqlSession session=getSqlSession(); UserInfoDao dao=session.getMapper(UserInfoDao.class);where id=102 name=#{name}, pwd=#{pwd}, rolesid=#{rolesid},