zoukankan      html  css  js  c++  java
  • MyBatis

    一、MyBatis核心配置文件mybatis-config.xml

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
     3         "http://mybatis.org/dtd/mybatis-3-config.dtd">
     4 <configuration> 
     5               <environments default="mysql"> 
     6                      <environment id="mysql"> 
     7                            <transactionManager type="JDBC" /> 
     8                                <dataSource type="POOLED">
     9                       <property name="driver" value="com.mysql.jdbc.Driver" />
    10                                   <property name="url" value="jdbc:mysql://localhost:3306/mybatis" />              
    11                       <property name="username" value="root" />
    12                       <property name="password" value="root" />
    13                                 </dataSource>
    14                            </environment>
    15                       </environments> 
    16              <mappers>
    17          <mapper resource="com/itheima/mapper/CustomerMapper.xml" />
    18              </mappers>
    19         </configuration>

     

    二、映射文件×××Mapper.xml

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
     3         "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
     4 <mapper namespace="com.wxy.mapper.CustomerMapper">
     5     <!--查-->
     6     <select id="findCustomerById" parameterType="Integer"
     7             resultType="com.wxy.pojo.Customer">
     8         select * from customer where id = #{id}
     9     </select>
    10     <select id="findCustomerByName" parameterType="String"
    11             resultType="com.wxy.pojo.Customer">
    12         select * from customer where username like concat('%',#{value},'%');
    13     </select>
    14     <select id="findCustomer" parameterType="com.wxy.pojo.Customer"
    15             resultType="com.wxy.pojo.Customer">
    16         select * from customer
    17     </select>
    18     <!--支持主键自增的数据库插入-->
    19     <insert id="addCustomer"  parameterType="com.wxy.pojo.Customer"
    20             keyProperty="id" useGeneratedKeys="true" >
    21         insert into customer(username,jobs,phone)
    22         values(#{username},#{jobs},#{phone})
    23     </insert>
    24     <!--不支持自增的数据库插入-->
    25     <insert id="addCustomer2"  parameterType="com.wxy.pojo.Customer">
    26         <selectKey keyProperty="id" order="AFTER" resultType="Integer">
    27             select last_insert_id()
    28         </selectKey>
    29         insert into customer(id,username,jobs,phone)
    30         values(#{id},#{username},#{jobs},#{phone})
    31     </insert>
    32     <!--改-->
    33     <update id="updateCustomer" parameterType="com.wxy.pojo.Customer">
    34         update customer set
    35         username=#{username},jobs=#{jobs},phone=#{phone}
    36         where id=#{id}
    37     </update>
    38     <!--删-->
    39     <delete id="deleteCustomer" parameterType="Integer">
    40         delete from customer where id=#{id}
    41     </delete>
    42 </mapper>

    三、MyBatis工作原理

     1 //1.读取配置文件
     2 String resource = "mybatis-config.xml";
     3 InputStream inputStream = Resources.getResourceAsStream(resource);
     4 //2.根据配置文件构建SqlSessionFactory
     5 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
     6 //3.通过SqlSessionFactory创建SqlSession
     7 SqlSession sqlSession = sqlSessionFactory.openSession();
     8 //4.SqlSession执行映射文件中定义的SQL语句,并返回映射结果
     9 //即:调用映射文件中不同的子元素id,执行相应的操作
    10 {
    11          Customer customer = sqlSession.selectOne("com.itheima.mapper"
    12                     + “.CustomerMapper.findCustomerById”, 1);          
    13 }         
    14 //5.关闭SqlSession
    15 sqlSession.close();

    log4j.properties日志文件:

    log4j.rootLogger=ERROR, stdout
    log4j.logger.com.wxy=DEBUG
    log4j.appender.stdout=org.apache.log4j.ConsoleAppender
    log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
    log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

  • 相关阅读:
    centos 远程授权
    jar 包启动脚本
    Java 优化要点
    Java 性能优化
    maven快速自动更新子模块项目的版本号
    spring boot Tomcat文件上传找不到零时文件夹
    redis 集群常用命令
    java基础知识点学习
    Markdown 语法
    elasticsearch
  • 原文地址:https://www.cnblogs.com/wxywxy/p/9008142.html
Copyright © 2011-2022 走看看