spring+spring mvc+mybatis
2018-07-20 来源:open-open
[Java]代码
package com.mytest.tesst; import java.io.InputStream; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import com.mytest.beans.User; public class MyBatisTest { public static void main(String[] args) { // TODO Auto-generated method stub String resource = "mybatis_config.xml"; // com.mysql.jdbc.Driver InputStream is = MyBatisTest.class.getClassLoader().getResourceAsStream(resource); SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is); SqlSession session = sessionFactory.openSession(); String statement = "com.mytest.dao.UserDao.getUser";//获取对应的sql替换 //?查询sql,获取结果,通过id去查询用户 User user = session.selectOne(statement, 1); System.out.println(user); //查询列表 List<User> userList = session.selectList("com.mytest.dao.UserDao.getList"); System.out.println("user_count:"+userList.size()); Map<String,String> map = new HashMap<String,String>(); map.put("name", "123"); map.put("age", "1234"); User user1 = session.selectOne("com.mytest.dao.UserDao.getUserByMap", map ); System.out.println("user1:"+user1); } }
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。
最新资讯
热门推荐