zoukankan      html  css  js  c++  java
  • Mybatis框架——day01

    一,日志log

      1.日志:依赖包log4j,创建log配置文件,debug提供的信息更多,stdout输出到控制台,file输出到文件

    ### direct log messages to stdout ###
    log4j.appender.stdout=org.apache.log4j.ConsoleAppender
    log4j.appender.stdout.Target=System.out
    log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
    log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
    
    ### direct messages to file mylog.log ###
    log4j.appender.file=org.apache.log4j.FileAppender
    log4j.appender.file.File=d:/mylog.log
    log4j.appender.file.layout=org.apache.log4j.PatternLayout
    log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
    
    ### set log levels - for more verbose logging change 'info' to 'debug' ###
    log4j.rootLogger=debug, stdout

     

    二,配置mybatis.xml文件,数据源和映射文件

    <configuration>
        <environments default="dev">
            <environment id="dev">
                <!--数据源-->
                <transactionManager type="JDBC"></transactionManager>
                <dataSource type="POOLED">
                    <property name="driver" value="com.mysql.jdbc.Driver"></property>
                    <property name="url" value="jdbc:mysql:///bookstore"></property>
                    <property name="username" value="root"></property>
                    <property name="password" value="123456"></property>
                </dataSource>
            </environment>
        </environments>
        <!--引入映射文件-->
        <mappers>
          //通过配置文件引入 <mapper resource="Person.xml"></mapper>
          //通过包引入,此包在resources下,编译后需要和src内的包同级 <package name="com.lanou.mapper"></package> </mappers> </configuration>
     

    三,映射文件xml的配置,手动配置bean和数据库表的映射关系

    <mapper namespace="person">
       <!--可以通过namespace.id调用sql语句,具体看测试类--> <resultMap id="personMap" type="com.lanou.entity.Person"> <!--id标签配置的是主键--> <!--手动映射关系,并且主键返回,插入到类的对象中--> <id property="id" column="pid"></id> <result property="name" column="name"></result> <result property="age" column="age"></result> </resultMap> <!--使用手动映射--> <select id="findAll" resultMap="personMap"> SELECT * FROM person </select> </mapper>
    <mapper namespace="com.lanou.mapper.PersonMapper">
        <resultMap id="personMap" type="com.lanou.entity.Person">
            <!--id标签配置的是主键-->
            <!--手动映射关系,并且主键返回,插入到类的对象中-->
            <id property="id" column="pid"></id>
            <result property="name" column="name"></result>
            <result property="age" column="age"></result>
        </resultMap>
        <!--使用手动映射-->
        <select id="findAll" resultMap="personMap">
            SELECT * FROM person
        </select>
        <!--传入的是pojo参数,参数名必须为bean的属性名-->
        <!--<insert id="insert" useGeneratedKeys="true" keyProperty="id">-->
            <!--INSERT INTO  person VALUES (null,#{name},#{age})-->
        <!--</insert>-->
        <!--自动映射,传入基本类型参数,#{随便写}-->
        <!--<select id="findById" resultType="com.lanou.entity.Person">-->
        <select id="findById" resultMap="personMap">
            SELECT * FROM person WHERE pid = #{id}
        </select>
    
    </mapper>

      接口代码:

    public interface PersonMapper {
        List<Person> findAll();
    
        int insert(Person person);
    
        List<Person> findByNameLike(String str);
    }

      测试代码:

    @Test
        public void test1() throws IOException {
            //加载全局配置文件
            InputStream resource = Resources.getResourceAsStream("mybatis.xml");
            //通过全局配置文件创建出SqlSessionFactory
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resource);
            //通过SqlSessionFactory获得SqlSession,SqlSession类似于JDBC中的Connection
            SqlSession sqlSession = sqlSessionFactory.openSession();
            //通过SqlSession执行sql语句,获得结果集
            //通过namespace.id 在映射文件中找到对应的sql
            List<Person> persons = sqlSession.selectList("person.findAll");
            for (Person person : persons) {
                System.out.println(person);
            }
        }

      

     
  • 相关阅读:
    使用 HttpClient 调用第三方接口
    Servlet 的生命周期及工作原理
    SpringBoot 框架简介及搭建
    使用 SimpleDateFormat 获取上个月的日期
    LoadRunner常见问题(一)
    Web_reg_find()函数的使用
    IEtester调试IE6,IE7,IE8,IE9的CSS兼容性的免费工具
    web_find()函数检查中文字符串失败的处理方法
    lr_shoami()的用法
    IP欺骗经验总结
  • 原文地址:https://www.cnblogs.com/memo-song/p/9179862.html
Copyright © 2011-2022 走看看