MyBatis学习总结(一)mybatis与spring整合
2024-09-07 18:08:34
MyBatis学习总结(一)mybatis与spring整合
一、需要的jar包
1、spring相关jar包
2、Mybatis相关的jar包
3、Spring+mybatis相关jar包
4、MySql驱动包
5、数据库连接池包
二、配置文件
1、参数文件(db.properties)
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8
jdbc.username=root
jdbc.password=root
2、spring相关(applicationContext.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!--读取配置文件-->
<context:property-placeholder location="classpath:db.properties"/>
<!--配置数据源-->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="maxActive" value="10" />
<property name="maxIdle" value="5" />
</bean>
<!--sqlSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:SqlMapConfig.xml" /> <!--name必须这么写-->
</bean>
<!--扫描包配置-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.mybatis.dao" /> <!--name必须这么写-->
</bean>
</beans>
3、Mybatis相关(SqlMapConfig.xml && Mapper.xml)
3.1 Mybatis核心配置文件
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!--起别名-->
<typeAliases>
<package name="com.mybatis.pojo" />
</typeAliases>
</configuration>
< typeAliases>说明:这个在此处是声明pojo的包路径,在mapper.xml文件中,对没个sql的parameterType和resultType可以直接使用pojo的类名,而不用写全路径,而且mapper中的类名和实际pojo的类名可以按照一定的规则不一定要完完全全相同,具体规则此处不赘述
3.2 Mapper映射文件
这只拿查询做一个例子,主要说明mybatis和spring整合的环境搭建
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.mybatis.dao.UserMapper">
<select id="queryById" parameterType="Integer" resultType="user">
select * from user where id = #{id};
</select>
</mapper>
4、测试方法
@Test
public void testMybatis(){
ApplicationContext ca = new ClassPathXmlApplicationContext("applicationContext.xml");
UserMapper userMaper = (UserMapper) ca.getBean("userMapper");
User user = userMaper.queryById(1);
System.out.println(user);
}
5、pojo类
package com.mybatis.pojo;
import java.util.Date;
public class User {
private String id;
private String username;
private String sex;
private Date birthday;
private String address;
private String detail;
private Integer score;
get/set...
toString
}
最新文章
- iOS 阶段学习第八天笔记(指针)
- sql sp_xml_preparedocument 函数运用实例
- PostgreSQL用户角色及其属性介绍
- App接口中json方式封装通信接口
- svn switch relocate用法
- 欢迎来到Googny的博客
- java: Eclipse jsp tomcat 环境搭建(完整)
- QT 强止杀进程
- Java 的强引用、弱引用、软引用、虚引用
- 通过Microsoft Learn进行学习以提升技能
- 1173 - The Vindictive Coach
- [转]Ubuntu18.04搜狗拼音输入法候选栏乱码解决方法
- Zabbix监控Nginx状态信息
- java中Integer 和String 之间的转换
- 手把手教你学node.js之学习使用外部模块
- JS中的this机制
- Java基础之instanceof和transient关键字用法
- 浏览器报错:unexpected end of input 解决方法
- JMeter 十四:最佳实践
- Spring框架实现——远程方法调用RMI代码演示
热门文章
- js过滤和包含数组方法
- linux下解压命令大全(转)
- Android 布局错乱 Android花屏
- RedHatEnterpriseLinuxServerRelease7.3上配置vsftpd服务器
- IOS客户端的个人中心可以查看自己的博客了。
- 剑指Offer - 九度1515 - 打印1到最大的N位数
- 《Cracking the Coding Interview》——第6章:智力题——题目3
- table内容超出宽度时隐藏并显示省略标记
- HDU 3954 Level up (线段树特殊懒惰标记)
- ocrosoft Contest1316 - 信奥编程之路~~~~~第三关 问题 x: 十进制到二进制的转换