博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
myBatis之入门示例
阅读量:4325 次
发布时间:2019-06-06

本文共 3552 字,大约阅读时间需要 11 分钟。

1. myBatis目录结构:

--src

       ---entity [POJO类]

       ---mappers [映射类]

          ----*Mapper.java [方法接口,相当于Dao]

          ----*Mapper.xml  [sql语句文件,相当于DaoImpl]

       ---utils [工具类]

       ---jdbc.properties [数据库信息文件]

      ---myBatis-config.xml [myBatis配置文件]

--lib [架包文件]

      ---mybatis-3.2.8.jar

      ---mysql-connector-java-5.1.7-bin.jar

 

 

 

 

 

 

 

 

 

 

 

 

2.entity

package com.blueStarWei.entity;public class TPersonInfo {        private Integer id;    private String name;    private Integer age;     //setter & getter         @Override    public String toString() {        return "TPersonInfo [id=" + id + ", name=" + name + ", age=" + age + "]";    }    }

 

3. mappers

    3.1 *Mapper.java

package com.blueStarWei.mappers;import java.util.List;import com.blueStarWei.entity.TPersonInfo;public interface PersonMapper {    /**     *      * @param person     * @return success ? 1 : 0     */    int create(TPersonInfo person);        int update(TPersonInfo person);        int delete(Integer id);        TPersonInfo findById(Integer id);        List
findAll();}

 

    3.2 *Mapper.xml

insert into t_person_info values(null,#{name},#{age})
update t_person_info set name = #{name}, age = #{age} where id = #{id}
delete from t_person_info where id = #{id}

 

4. utils

package com.blueStarWei.utils;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;public class SqlSessionFactoryUtil {    private static SqlSessionFactory sessionFactory;        public static SqlSessionFactory getSqlSessionFactory(){        if(sessionFactory == null){            InputStream inputStream;            try {                inputStream = Resources.getResourceAsStream("mybatis-config.xml");                sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);            } catch (IOException e) {                e.printStackTrace();            }        }        return sessionFactory;    }        public static SqlSession openSession(){        return getSqlSessionFactory().openSession();    }}

 

5. jdbc.properties

jdbc.driverClassName=com.mysql.jdbc.Driverjdbc.url=jdbc:mysql://localhost:3306/db_mybatisjdbc.username=rootjdbc.password=root

 

6. mybatis-config.xml

      6.1 配置文件讲解:

            6.1.1 environments  : MyBatis 支持多个环境,可以任意配置;

            6.1.2 transactionManager : MyBatis 支持两种类型的事务管理器:JDBC 和 MANAGED(托管);

                  6.1.2.1 JDBC:应用程序负责管理数据库连接的生命周期

                  6.1.2.2 MANAGED:由应用服务器负责管理数据库连接的生命周期;(一般商业服务器才有此功能,如 JBOSS,WebLogic)

            6.1.3 dataSource : 用来配置数据源;类型有:UNPOOLED,POOLED,JNDI;

                  6.1.3.1 UNPOOLED : 没有连接池,每次数据库操作,MyBatis 都会创建一个新的连接,用完后,关闭;适合小并发项目.

                  6.1.3.2 POOLED : 连接池,每次数据库操作,MyBatis 都会从连接池中获取链式,可用于高并发项目。【推荐】

                  6.1.3.3 JNDI,使用应用服务器配置 JNDI 数据源获取数据库连接

7. lib

    获取目录:https://pan.baidu.com/s/1248sNRHReiK8S3pENKfmlA  密码:xpe7

 

    更多内容,请访问:

 

转载于:https://www.cnblogs.com/BlueStarWei/p/8639448.html

你可能感兴趣的文章
npm安装vue-cli时速度慢,fetchMetadata经常卡住并报异常
查看>>
POJ:1703-Find them, Catch them(并查集好题)(种类并查集)
查看>>
HDU:5040-Instrusive
查看>>
校验器
查看>>
thread/threading——Python多线程入门笔记
查看>>
linux 命令汇总(搜索、fdfs、常用命令),虚拟机dump文件
查看>>
Nginx 反向代理解决浏览器跨域问题
查看>>
为什么现在我最终推荐内存OLTP
查看>>
git error: failed to push some refs to...
查看>>
Markdown指南
查看>>
influxDB的安装和简单使用
查看>>
JPA框架学习
查看>>
JPA、JTA、XA相关索引
查看>>
机器分配
查看>>
php opcode缓存
查看>>
springcloud之Feign、ribbon设置超时时间和重试机制的总结
查看>>
Go 结构体
查看>>
LINQ巩固
查看>>
观看杨老师(杨旭)Asp.Net Core MVC入门教程记录
查看>>
优化后的二次测试Miller_Rabin素性测试算法
查看>>