MyBatis_Day1_MyBatis、增删改查、手动映射、SQL注入问题、别名配置

MyBatis

Mybatis原名iBatis,是Apache的一个开源项目,2010年从apache software foundation 迁移到Google code并改名,2013年11月迁移至Github

MyBatis是一款优秀的持久层框架,支持定制化SQL、储存过程以及高级映射。MyBatis避免了几乎所有的JDBC代码和手动设置参数以及获取结果集。

MyBatis是对JDBC的封装,用于简化我们DAO层的开发

MyBatis提供的高级特性:动态SQL、一级缓存、二级缓存、日志管理、多表级联查询、批处理等

MyBatis官网:https://mybatis.org/mybatis-3/zh/index.html

Github官网:https://github.com/mybatis/mybatis-3/releases

环境搭建

MAVEN依赖

    <!-- mybatis依赖 -->
    <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.5</version>
    </dependency>
        
    <!--mysql依赖-->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.40</version>
    </dependency>
​
    <!--日志依赖-->
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.17</version>
    </dependency>

resources.MyBatisConfig.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">//如果报错识别不到约束可以Alt+Enter:fetch external resource
        
<configuration>
​
    <settings>
        <!--日志配置-->
        <setting name="logImpl" value="STDOUT_LOGGING"/>
        <!--开启驼峰命名自动映射-->
        <setting name="mapUnderscoreToCamelCase" value="true"/>
    </settings>
    
    <!--  default 默认使用那个运行环境 -->
    <environments default="dev">
    
        <!--配置dev运行环境-->
        <environment id="dev">
        
            <!--事务管理器:JDBC事务管理-->
            <transactionManager type="JDBC"></transactionManager>
            
            <!--POOLED指定采用mybatis内置的连接池支持-->
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver"></property>
                <property name="url" value="jdbc:mysql://localhost:3306/test?characterEncoding=UTF8"></property>
                <property name="username" value="root"></property>
                <property name="password" value="admin"></property>
            </dataSource>
            
        </environment>
    </environments>
</configuration>

EmpDao接口

public interface EmpDao {
    List<Emp> findAll();
}

在resources中建立Dao的同级目录EmpDaoxml

<?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">
​
<!--namespace 名称空间,指定对哪个接口进行映射-->
<mapper namespace="com.xxx.dao.EmpDao">
    <!--
        select 表示执行查询sql
            id 唯一; 必须与接口中方法名称一致。
            resultType 表示执行查询SQL封装的实体类对象。如果放回集合,只需要指定集合中元素的类型即可。
            标签体中存放要执行的select语句 
    -->
    <select id="findAll" resultType="com.xxx.entity.Emp">
        select * from emp
    </select>
</mapper>

在MyBatisConfig.xml中添加接口映射

<mappers>
    <!--加载mapper映射文件-->
    <mapper resource="com/xxx/dao/EmpDao.xml"></mapper>
</mappers>

测试代码

public class Demo01 {
​
    @Test
    public void test1() throws Exception {
​
        // 1. 加载mybatis的核心配置文件
        InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");
​
        // 2. 根据核心配置文件创建一个SqlSession工厂
        SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
        SqlSessionFactory sessionFactory = sqlSessionFactoryBuilder.build(is);
​
        // 3. 使用工厂对象创建SqlSession
        SqlSession session = sessionFactory.openSession();
​
        // 4. 通过session获取对应的Mapper对象
        EmpDao empDao = session.getMapper(EmpDao.class);
​
        // 5. 调用方法执行查询
        List<Emp> userList = empDao.findAll();
​
        for (Emp emp : userList) {
            System.out.println(emp);
        }
​
        is.close();
        session.close();
    }
}

增删改查

Dao.xml

<insert> SQL </insert>
​
<delete> SQL </delete>
​
<update> SQL </update>
​
<select> SQL </select>

参数:

id="..." -- 该namespace指向的方法

parameterType="..." -- 该namespace传入的参数

resultType="..." -- 该namespace传出的参数

注:parameterType和resultType对于未配置别名的实体类需要输入完整的包名如

如何手动映射

自定义手动映射结果集

<resultMap id="empResultMap" type="com.dfbz.entity.Emp">
    <!--id列的映射用id标签-->
    <id column="id" property="id"></id>
​
    <!--其他列的映射通通使用result标签,把column的值映射到property上-->
    <result column="e_name" property="name"></result>
    <result column="age" property="age"></result>
    <result column="e_addr" property="addr"></result>
    <result column="salary" property="salary"></result>
</resultMap>
​
<select id="findById" resultMap="empResultMap">
      select id,name e_name,age,addr e_addr,salary from emp where id=#{id}
</select>

SQL注入问题

1)#{...}会对特殊字符进行过滤,传递的参数不会参与SQL语句的生成。

2)${...}不会对特殊字符进行过滤,传递的参数本就是用于参与SQL语句生成的,因此会出现SQL注入的问题

3)${...}里面必须填写value(新版本已经改进),#{...}随意

别名配置

在MyBatisConfig.xml中添加配置:

注:别名不区分大小写

<typeAliases>
    <!--配置包别名-->
    <package name="com.xxx.entity"/>
    <!--配置单个别名-->
<!--        <typeAlias type="com.xxx.entity.Emp" alias="emp"></typeAlias>-->
</typeAliases>

  • 5
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值