基于SpringBoot myBatis连接数据库暨操作数据库

连接数据库

引入依赖

在创建springboot工程的时候引入Mybatis和Mysql驱动

引入德鲁伊连接池

<dependency>
    <!-- Druid连接池依赖 -->
    <groupId>com.alibaba</groupId>
    <artifactId>druid-spring-boot-starter</artifactId>
    <version>1.2.8</version>
</dependency>

 

在application.properties中配置数据库的连接信息

1.连接数据库

打开这个文件

下面代码中localhost要替换成自己数据库服务器的地址-3306是数据库的端口号 查看一下自己的端口号是多少---mybatis 是自己创建的数据库名称

#驱动类名称
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#数据库连接的url
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis        
#连接数据库的用户名
spring.datasource.username=root
#连接数据库的密码
spring.datasource.password=1234

#配置mybatis的日志, 指定输出到控制台
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl

#开启mybatis的驼峰命名自动映射开关 a_column ------> aCloumn
mybatis.configuration.map-underscore-to-camel-case=true

创建一个UserMapper接口用来查询数据 

@Mapper        //这一步会自动将查询到的数据封装成List对象并且 放到bean容器中
public interface UserMapper {

    @Select("select * from user")    //里面操作数据库的语句
    public List<User> list();       
}

 新建一个类,注入UserMapper对象,执行sql操作

@Autowired    //容器注入
private UserMapper userMapper;

public void testListUser(){
        List<User> userList = userMapper.list();
        userList.stream().forEach(user -> {
            System.out.println(user);
        });
    }

操作数据库

1,基于注解操作数据库

@Mapper
public interface EmpMapper {

    @Insert("insert into emp(username, name, gender, image, job, entrydate, dept_id, create_time, update_time) values (#{username}, #{name}, #{gender}, #{image}, #{job}, #{entrydate}, #{deptId}, #{createTime}, #{updateTime})")
    public void insert(Emp emp);

}

2,参数占位符

  • #{...}                ---------------------一般使用这个,不会存在sql注入问题

    • 执行SQL时,会将#{…}替换为?,生成预编译SQL,会自动设置参数值

    • 使用时机:参数传递,都使用#{…}

  • ${...}

    • 拼接SQL。直接将参数拼接在SQL语句中,存在SQL注入问题

    • 使用时机:如果对表名、列表进行动态设置时使用

3,为解决sql注入问题 引入 concat()字符串拼接函数

使用方法:

select * from user where name like concat('%','张','%');

4,XML映射文件操作数据库 

  • XML映射文件的名称与Mapper接口名称一致,并且将XML映射文件和Mapper接口放置在相同包下(同包同名)

  • XML映射文件的namespace属性为Mapper接口全类名一致
  • XML映射文件中sql语句的id与Mapper接口中的方法名一致,并保持返回类型一致。

resultType指的是单条记录所封装的类型

resultMap指的是多条记录所封装的类型

<select id="limitList" resultType="com.itheima.pojo.Student">
        SELECT * from student
        <where>
            <if test="name!=null">
                name like concat('%',#{name},'%')
            </if>
            <if test="educationId!=null">
                 and education_id=#{educationId}
            </if>
            <if test="number!=null">
                and number like concat('%',#{number},'%')
            </if>
            <if test="classId!=null">
                and class_id=#{classId}
            </if>
        </where>
</select>

mybatisx插件 在IDEA中用

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

奋进的大马猴

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值