zoukankan      html  css  js  c++  java
  • mybits(1)配置

    mybits-config.xml

    <?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="mysql">
            <!-- 配置mysql数据库 -->
            <environment id="mysql">
                <!-- JDBC事物管理 -->
                <transactionManager type="JDBC" />
                <!-- 数据库连接池 -->
                <dataSource type="POOLED">
                    <property name="driver" value="com.mysql.jdbc.Driver" />
                    <property name="url" value="jdbc:mysql://localhost:3306/mybits" />
                    <property name="username" value="root" />
                    <property name="password" value="123456" />
                </dataSource>
            </environment>
        </environments>
        <mappers>
            <mapper resource="com/kaibing/mapper/CustomerMapper.xml" />
        </mappers>
    </configuration>

    CustomerMapper.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="com.kaibing.mapper.CustomerMapper">
         <select id="findCustomerById" parameterType="Integer" 
             resultType="com.kaibing.pojo.Customer">
         select * from t_customer where id = #{id}
         </select>
    </mapper>

     Customer.java

    package com.kaibing.pojo;
    
    public class Customer {
        private Integer id;
        private String username;
        private String jobs;
        private String phone;
        public Integer getId() {
            return id;
        }
        public void setId(Integer id) {
            this.id = id;
        }
        public String getUsername() {
            return username;
        }
        public void setUsername(String username) {
            this.username = username;
        }
        public String getJobs() {
            return jobs;
        }
        public void setJobs(String jobs) {
            this.jobs = jobs;
        }
        public String getPhone() {
            return phone;
        }
        public void setPhone(String phone) {
            this.phone = phone;
        }
        @Override
        public String toString() {
            return "Customer [id=" + id + ", username=" + username + ", jobs=" + jobs + ", phone=" + phone + "]";
        }
        
        
        
    
    }

    MybitsTest.java

    package com.kaibing.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 com.kaibing.pojo.Customer;
    
    public class MybitsTest {
        
        public static void main(String[] args) {
            String resource = "mybits-config.xml";
    //        读取配置文件
            InputStream inputStream=null;
            try {
                inputStream = Resources.getResourceAsStream(resource);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            
    //        构建配置文件工厂
            SqlSessionFactory sqlSessionFactory = 
                    new SqlSessionFactoryBuilder().build(inputStream);
    //        创建会话
            SqlSession sqlSession = sqlSessionFactory.openSession();
    //        查询
            Customer customer =  sqlSession.selectOne("com.kaibing.mapper.CustomerMapper"
                    + ".findCustomerById",1);
    //        输出结果
            System.out.println(customer.toString());
            
    //        关闭会话
            sqlSession.close();
        }
        
    
    }

     配置log4j

    log4j.properties

    # Global logging configuration
    log4j.rootLogger=ERROR, stdout
    # MyBatis logging configuration...
    log4j.logger.com.kaibing=DEBUG
    # Console output...
    log4j.appender.stdout=org.apache.log4j.ConsoleAppender
    log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
    log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
  • 相关阅读:
    [LeetCode]Binary Tree Level Order Traversal
    [LeetCode]Binary Tree Postorder Traversal
    Netty(六):NioServerSocketChannel源码解析
    Netty(五):ServerBootstrap启动流程
    Netty(四):AbstractChannel源码解析
    Netty(三):IdleStateHandler源码解析
    自定义fastjson对枚举类型的序列化及反序列化过程
    TCP连接过程及报文解析
    Netty(二):数据在ChannelPipeline中的流经
    Netty(一):ByteBuf读写过程图解
  • 原文地址:https://www.cnblogs.com/kaibing/p/9100567.html
Copyright © 2011-2022 走看看