myBatis也是一个持久型框架,相较于hibernate来说,算是轻量级的。

1.配置mybatis环境

  相关jar下载地址:mybatis+mysalJAR包

2.新建一个java project工程

2.1配置log4j.properties文件,用于日志输出

 log4j.rootCategory=DEBUG, Console

 log4j.appender.Console=org.apache.log4j.ConsoleAppender
 log4j.appender.Console.layout = org.apache.log4j.PatternLayout
 log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] -%m%n

 log4j.logger.java.sql.ResultSet = INFO
 log4j.logger.org.apache = INFO
 log4j.logger.java.sql.Connection = DEBUG
 log4j.logger.java.sql.Statement = DEBUG
 log4j.logger.java.sql.PreparedStatement = DEBUG

log4j

2.2配置MybatisConfig.xml,用于配置数据库连接

 <?xml version="1.0" encoding="UTF-8"?>

 <!DOCTYPE configuration
  PUBLIC "-//mynatis.org//DTD Config 3.0 EN"
  "http://mybatis.org/dtd/mybatis-3-config.dtd">

 <configuration>
     <!-- 别名 -->
     <typeAliases>
         <typeAlias alias="User" type="com.juin.entity.User" />
         <typeAlias alias="Person" type="com.juin.entity.Person" />
     </typeAliases>

     <environments default="development">
     <environment id="development">
     <transactionManager type="JDBC">

     </transactionManager>
     <dataSource type="POOLED">
     <property name="driver" value="com.mysql.jdbc.Driver"/>
     <property name="url" value="jdbc:mysql://localhost:3306/mybatis_test"/>
     <property name="username" value="juin"/>
     <property name="password" value="123654"/>
     </dataSource>
     </environment>
     </environments>

     <mappers>
         <mapper resource="com/juin/map/juinUser.xml"/>
         <mapper resource="com/juin/map/person.xml"/>
         <mapper class="com.juin.map.IUser"/>
     </mappers>
 </configuration>

MyBatisConfig.xml

2.3配置juinUser.xml,前面忘记说了,mybatis实现了代码与数据库连接语句的分离。

 <?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="domain.blog.mappers.AuthorMapper">
     <select id="findById" parameterType="int" resultType="com.juin.entity.User">
         select * from user where id=#{id}
     </select>

     <insert id="insertUser" parameterType="User" statementType="PREPARED"
         keyProperty = "id" useGeneratedKeys = "true">
         insert into user (username,psw) values
         (#{username},#{psw})
     </insert>

     <update id="updateUser" parameterType="User">
         UPDATE User SET
         username=#{username},
         psw=#{psw}
         where id = #{id}
     </update>

     <select id="loginSelect" parameterType="hashmap" resultType="User">
         select * from user where username = #{username} and psw = #{psw}
     </select>

     <select id="loginSelect2" parameterType="User" resultType="User">
         select * from user where username = #{username} and psw = #{psw}
     </select>

     <select id="selectList" resultType="User">
         select * from user
     </select>

     <resultMap id="UserMap" type="User">
         <id property="id" column="id"/>
         <result property="username" column="username"/>
         <result property="psw" column="psw"/>
     </resultMap>
     <select id="selectUsers" resultMap="UserMap">
         select * from user
     </select>
 </mapper>

juinUser.xml

2.4创建User实体类

 package com.juin.entity;

 public class User {
     private int id;
     private String username;
     private String psw;

     public int getId() {
         return id;
     }

     public void setId(int id) {
         this.id = id;
     }

     public String getUsername() {
         return username;
     }

     public void setUsername(String username) {
         this.username = username;
     }

     public String getPsw() {
         return psw;
     }

     public void setPsw(String psw) {
         this.psw = psw;
     }
 }

User.java

2.5创建测试类

 package com.juin.test;

 import java.io.IOException;
 import java.io.Reader;
 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.juin.entity.User;

 public class test1 {

     public static void main(String[] args) {

         String resource = "com/juin/map/MybatisConfig.xml";
         Reader reader = null;
         SqlSession session = null;

         try {
             reader = Resources.getResourceAsReader(resource);
         } catch (IOException e) {
             // TODO Auto-generated catch block
             e.printStackTrace();
         }
         SqlSessionFactory sqlMapper = new SqlSessionFactoryBuilder().build(reader);
         session = sqlMapper.openSession();
         User temp = session.selectOne("findById", 2);
         System.out.println(temp.getUsername());
         session.close();
     }

 }

test

myBatis学习之路1-基本功能实现的更多相关文章

  1. mybatis学习之路----批量更新数据两种方法效率对比

    原文:https://blog.csdn.net/xu1916659422/article/details/77971696/ 上节探讨了批量新增数据,这节探讨批量更新数据两种写法的效率问题. 实现方 ...

  2. mybatis学习之路

    MyBatis 是支持普通SQL查询.存储过程和高级映射的优秀持久层框架. MyBatis消除了几乎所有的JDBC代码和参数的手工设置以及对结果集的检索封装. MyBatis可以使用简单的XML或注解 ...

  3. MyBatis学习之路之configuration配置

    1.首先讲解的是MyBatis核心配置文件configuration.xml的配置 一个完整的configuration.xml配置顺序如下: properties,settings,typeAlia ...

  4. mybatis学习之路----mysql批量新增数据

    原文:https://blog.csdn.net/xu1916659422/article/details/77971867 接下来两节要探讨的是批量插入和批量更新,因为这两种操作在企业中也经常用到. ...

  5. springboot 学习之路 3( 集成mybatis )

    目录:[持续更新.....] spring 部分常用注解 spring boot 学习之路1(简单入门) spring boot 学习之路2(注解介绍) spring boot 学习之路3( 集成my ...

  6. 中小研发团队架构实践之生产环境诊断工具WinDbg 三分钟学会.NET微服务之Polly 使用.Net Core+IView+Vue集成上传图片功能 Fiddler原理~知多少? ABP框架(asp.net core 2.X+Vue)模板项目学习之路(一) C#程序中设置全局代理(Global Proxy) WCF 4.0 使用说明 如何在IIS上发布,并能正常访问

    中小研发团队架构实践之生产环境诊断工具WinDbg 生产环境偶尔会出现一些异常问题,WinDbg或GDB是解决此类问题的利器.调试工具WinDbg如同医生的听诊器,是系统生病时做问题诊断的逆向分析工具 ...

  7. 新篇章之我的java学习之路下

    昨天写下了人生的第一篇博客,今天接着写我的java学习之路有关开发及框架的学习过程. 想要学好java语言,只学习一些java的基本语法对实际开发中的用处还是不大的,所以我们还要掌握一些有关javaW ...

  8. springboot 学习之路 1(简单入门)

    目录:[持续更新.....] spring 部分常用注解 spring boot 学习之路1(简单入门) spring boot 学习之路2(注解介绍) spring boot 学习之路3( 集成my ...

  9. springboot 学习之路 4(日志输出)

    目录:[持续更新.....] spring 部分常用注解 spring boot 学习之路1(简单入门) spring boot 学习之路2(注解介绍) spring boot 学习之路3( 集成my ...

随机推荐

  1. 安装Conda并在Conda下安装jupyter notebook

    1:安装 conda install jupyter notebook 2:启动 jupyter notebook

  2. Ubuntu系统安装nginx

    1.首先查看linux系统 cat /proc/version Linux version 4.9.59-v7+ (dc4@dc4-XPS13-9333) (gcc version 4.9.3 (cr ...

  3. 如何使用Vue-cli搭建和运行vue项目

    此文章  主要参考:https://jingyan.baidu.com/article/5225f26bbb430fe6fa0908ce.html 在vue init webpack my-proje ...

  4. web端MSF搭建

    去购买一个廉价VPS  阿里X/tx学生服务器然后选择Ubuntu系统http://jingyan.baidu.com/article/2c8c281dabacad0008252aa6.html安装M ...

  5. ubuntu14.04 LTS 更新国内网易163源

    2015/10/7 更改ubuntu的默认源是linux学习中必须掌握的基础技能.在此记录,以作参考. 在ubuntu14.04 LTS默认使用的是国外源,由于网络的原因,使用apt-get安装包时异 ...

  6. 限时免费 | 12月6日,广州保利洲际酒店,ABC Summit 2018云智峰会来了!

    随着科技的迅猛发展,人工智能技术也逐渐取得了各个突破.自20世纪70年代以来,作为计算机学科的一个分支,人工智能就被列为世界三大尖端技术之一.近年来,阿尔法狗战胜世界第一柯洁,使人工智能再度迎来新的热 ...

  7. phpspider php爬虫框架

    其实我自身的不是经常写正则,而且不规则的html去写正则本身就是件很麻烦的事情,如果页面有些微变动和更新就得再次去维护正则表达式,其实是非常蛋疼的 我第一感觉就是去找一下爬虫的库,但是发现现在php爬 ...

  8. ASP.NET Core ResponseCaching:基于 VaryByHeader 定制缓存 Key

    ASP.NET Core ResponseCaching 提供了缓存http响应内容的能力,通过它可以在本地内存中直接缓存http响应内容,这是速度最快的服务端缓存,省却了网络传输与生成响应内容的开销 ...

  9. 终于等到你!WebOptimizer - A bundler and minifier for ASP.NET Core

    迷人的 ASP.NET Core 有一个美中不足之处,自从一开始接触它到现在,我就一直不喜欢,一直想找到替代品,甚至想过自己实现一个,它就是 BundlerMinifier . 昨天面对 bundle ...

  10. python摸爬滚打之day15----初识类

    1.面向对象和面向过程 面向过程: 以事物的流程为核心.  优点: 负责的事物流程化, 编写简单; 缺点: 可拓展性差. 面向对象: 一切以对象为核心. 对象里封装着一切. 优点: 可拓展性强; 缺点 ...