Day02:②在Eclipse上配置Spring mybatis

本文介绍如何使用Maven搭建MyBatis环境,并通过示例演示如何进行基本的数据库操作。包括配置文件的设置、DAO接口定义及实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

接上一篇 Day02:①将服务器端数据通过 json 传到客户端

MyBatis 是支持定制化 SQL、存储过程以及高级映射的优秀的持久层框架。MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集。MyBatis 可以对配置和原生 Map 使用简单的 XML 或注解,将接口和 Java 的 POJOs(Plain Old Java Objects, 普通的 Java 对象) 映射成数据库中的记录。这段是摘取官方文档的。
这里我的数据库 sql 文件百度云链接
其实自己看着官方文档也可以配置,但可能会比较费事,为了巩固,特在此记录。

在第一步之前将web.xml文件中将classpath:spring-service.xml改为classpath:spring-*.xml

 <init-param>
      <description></description>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:spring-*.xml</param-value>
    </init-param>
1. 第一步,还是用过 maven 导入相关的包,通过阿里云 maven 库。这里我导入的包和响应的版本号如下,可以直接在 maven 库搜索,找到相应的 maven 坐标
  • mybatis3.4.0
  • mybatis-spring1.3.1
  • mysql-connector
  • java5.1.38
  • spring-jdbc3.2.8.RELEASE
    复制到 pom.xml 文件,保存,等一会儿,就会看到相应的包已加载。
2. 编写 spring-mybatis.xml 配置文件,内容如下
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:jdbc="http://www.springframework.org/schema/jdbc"  
    xmlns:jee="http://www.springframework.org/schema/jee" 
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
        http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">

    <!-- 配置到dbcp连接池:连接到数据库 -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <!-- 连接池基本连接参数 -->
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
        <property name="url" value="jdbc:mysql://localhost:3306/cloud_note"></property>
        <property name="username" value="root"></property>
        <property name="password" value="30940660"></property>
        <!-- 连接池可选参数 -->
        <property name="maxActive" value="50"></property>
        <property name="maxIdle" value="5"></property>  
    </bean>
    <!-- 配置MyBatis的Session工厂 -->
    <bean id="SqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <!-- 声明MyBatis SQL声明文件保存的地方 --> 
        <property name="mapperLocations" value="classpath:mapper/*.xml"></property>
    </bean>
    <!-- 配置MyBatis的自动接口扫描 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.lsy.dao"></property>    
    </bean>
</beans>
3. 创建 com.lsy.dao 包,这里包名要和 “配置 MyBatis 的自动接口扫描” 的包名一样,编写 UserDao 类
public interface UserDao {
    /**
     * 按照用户名统计用户个数
     * 
     * select count(*) from cn_user where cn_user_name=?
     * @param name
     * @return
     */
    int countUserByUserName(String name);

}
4. 创建测试的 demo

代码如下

package com.lsy.demo;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.lsy.dao.UserDao;
public class MyBatisDemo {
    public static void main(String[] args) {
        //初始化Spring
        ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("spring-mybatis.xml");
        UserDao dao = ctx.getBean("userDao", UserDao.class);
        int n = dao.countUserByUserName("demo");
        System.out.println(n);
    }
}
5. 最后的测试,先启动 tomcat,在运行 main 程序,可以看到在控制台打印 1,则 mybatis 配置成功且查找数据库成功。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值