接上一篇 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);
}
}