Mybatis

作者: 顶儿响叮当 | 来源:发表于2016-10-27 10:45 被阅读14次

    做了个mybatis的Demo,发现mybatis和我之前实习时用的EOS开发工具中的对数据库操作的组件很类似,现在想来那个EOS就是对mybatis进行了封装。

    eclipse下的Demo

    数据库表

    与数据表映射的实体类

    //与数据表映射的实体类
    package dao;
    
    public class User {
        private int id;
        private String name;
        private int age;
        
        public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public int getAge() {
            return age;
        }
        public void setAge(int age) {
            this.age = age;
        }
         
        @Override
        public String toString() {
           return "User [id=" + id + ", name=" + name + ", age=" + age + "]";
        }
    }
    
    

    userMapper.xml

    <!--userMapper.xml-->
    
    <?xml version="1.0" encoding="UTF-8" ?>
     <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
     <mapper namespace="mapping.userMapper">
         <!-- parameterType为传入参数类型,resultType为结果返回接收的类型 -->
         <select id="getUser" parameterType="int" 
             resultType="dao.User">
             select * from users
         </select>
         
     </mapper>
    

    mybatis配置文件

    <!--  mybatis配置文件 -->
    
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
    <configuration>
        <environments default="development">
            <environment id="development">
                <transactionManager type="JDBC" />
                <!-- 配置数据库连接信息 -->
                 <dataSource type="POOLED">
                    <property name="driver" value="com.mysql.jdbc.Driver" />
                    <property name="url" value="jdbc:mysql://localhost:3306/mybatis" />
                    <property name="username" value="root" />
                    <property name="password" value="000000" />
                </dataSource>
            </environment>
        </environments>
       
        <mappers>
            <!-- 注册userMapper.xml文件 -->
            <mapper resource="mapping/userMapper.xml"/>
        </mappers>
        
    </configuration>
    

    测试类

    public class Test {
    
         public static void main(String[] args) {
            // TODO Auto-generated method stub
          //mybatis的配置文件
            String resource = "conf.xml";
            //使用类加载器加载mybatis的配置文件(它也加载关联的映射文件)
            InputStream is = Test.class.getClassLoader().getResourceAsStream(resource);
            //构建sqlSession的工厂
            SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is);
            //使用MyBatis提供的Resources类加载mybatis的配置文件(它也加载关联的映射文件)
            //Reader reader = Resources.getResourceAsReader(resource); 
            //构建sqlSession的工厂
            //SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);
            //创建能执行映射文件中sql的sqlSession
            
            SqlSession session = sessionFactory.openSession();
    
            String statement = "mapping.userMapper.getUser";//映射sql的标识字符串
    
            List<User> userList = session.selectList(statement);
            
            for(int i = 0;i<userList.size();i++){
                System.out.println(userList.get(i).toString());
    
            }
        }
    }
    
    

    以上输出结果是user表的所有内容。

    mapper.xml的sql语句中可以随意的设置parameterType传入参数,是基本类型也可以是自定义的类,查询结果接收类型resultType也可以随便设置只要sql查询出的结果的属性能够满足是resultType属性的子集就可以(sql结果的属性name在resultType中都能有与之相对应的name)。

    //mapper中设置
     <mapper namespace="mapping.userMapper">
    
         <select id="getUser" parameterType="dao.User" 
             resultType="dao.User">
             select * from users where id=#{id} and name = #{name}
         </select>
         
     </mapper>
    
    
    
    
    
    //java中调用
            User user = new User();
            user.setAge(1);
            user.setId(1);
            user.setName("johnny");
    //        HashMap<String, Object> map = new HashMap<String, Object>();
    //        map.put("id", 2);
    //        map.put("name", "asd");
            List<User> userList = session.selectList(statement, user);
            
            for(int i = 0;i<userList.size();i++){
                System.out.println(userList.get(i).toString());
    
            }
    
    
    

    相关文章

      网友评论

          本文标题:Mybatis

          本文链接:https://www.haomeiwen.com/subject/ygbjuttx.html