
SSM中MyBatis使用XML配置文件版和注解版(eclipse)
发布日期:2021-05-07 08:40:57
浏览次数:24
分类:原创文章
本文共 5473 字,大约阅读时间需要 18 分钟。
环境:JDK1.8、elipse
1.导入包
2.创建实体类
当前
User
类中的内容:
public class User { private Integer uid; private String uname; private Integer money; ....省略getter和setter方法}
3.创建dao层以及对应的xml文件
UserMapper
中的内容:
public interface UserMapper { List<User> findAll();}
UserMapper.xml
文件中的内容:
<?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"><mapper namespace="com.hy.ssm.xml.mybatis.demo.dao.UserMapper"> <select id="findAll" resultType="User"> select * from users; </select></mapper>
注意当前的UserMapper类和UserMapper.xml在同一目录
4.创建controller层
当前的
UserController
中的内容:
@Controllerpublic class UserController { @Autowired UserMapper userMapper; @RequestMapping("/findAll") public String index(Model model) { List<User> findAll = userMapper.findAll(); model.addAttribute("items", findAll); return "index.jsp"; }}
5.在当前项目中添加applicationContext.xml文件
创建resources文件,并标记
当前的
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" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd "> <context:component-scan base-package="com.hy.ssm.xml.mybatis.demo"/> <context:property-placeholder location="classpath:db.properties"/> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="${driverClassName}"/> <property name="url" value="${url}"/> <property name="username" value="${username}"/> <property name="password" value="${password}"/> </bean> <bean id="sqlSession" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="typeAliasesPackage" value="com.hy.ssm.xml.mybatis.demo.entity"></property> <property name="dataSource" ref="dataSource"></property> </bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.hy.ssm.xml.mybatis.demo.dao"></property> </bean> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <mvc:annotation-driven/> </beans>
当前的
db.properties
文件中的内容:
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/test
username=root
password=root
6.配置web.xml文件
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>ssm-mybatis-xml</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <filter> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <servlet> <servlet-name>mvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>mvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping></web-app>
7.创建index页面
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %><!DOCTYPE html><html><head><meta charset="UTF-8"/><title>Insert title here</title></head><body> <table border="1"> <tr> <th>编号</th> <th>姓名</th> <th>金额</th> </tr> <c:forEach items="${items}" var="item"> <tr> <td>${item.uid}</td> <td>${item.uname}</td> <td>${item.money}</td> </tr> </c:forEach> </table></body></html>
8.启动和访问
结果成功!
9.使用mybatis注解版写sql
当前的
UserMapper
类中的内容:
public interface UserMapper { @Select("select * from users") List<User> findAll();}
删除UserMapper.xml文件
10.启动和访问
结果成功!
11.总结
1.使用SqlSessionFactoryBean中的basePackage
用于处理当前的dao层和xml之间的管理,并创建对应的dao的代理
2.当前的dao文件和xml的文件名称必须一致
3.使用MapperScannerConfigurer
中的typeAliasesPackage用来做别名映射
4.使用注解版的Mybatis的时候,直接删除xml文件
,直接写@Select等注解
,其余不变即可!
5.再次编写SSM项目并且使用xml注解配置还是那么熟悉,写当前的demo用于回忆作用,免得以后忘记
以上纯属个人见解,如有问题请联系本人!
发表评论
最新留言
初次前来,多多关照!
[***.217.46.12]2025年04月05日 17时09分41秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
Oracle删除主表数据
2019-03-05
js中两种定时器,setTimeout和setInterval实现验证码发送
2019-03-05
Oracle常用SQL
2019-03-05
JDK安装与环境变量配置(详细基础篇)
2019-03-05
golang内存及GC分析简易方法
2019-03-05
技术美术面试问题整理
2019-03-05
Redis分布式锁原理
2019-03-05
学习SSM中ajax如何与后台传数据
2019-03-05
【备份】求极限笔记
2019-03-05
【备份】概率论笔记备份
2019-03-05
ES6模块化与commonJS的对比
2019-03-05
C++学习记录 四、基于多态的企业职工系统
2019-03-05
C++学习记录 五、C++提高编程(2)
2019-03-05
面试问道nginx优化怎么做的
2019-03-05
自学linux毕业shell面试题
2019-03-05
4 Java 访问控制符号的范围
2019-03-05
第9章 - 有没有替代原因(检验证据)
2019-03-05
VUE3(八)setup与ref函数
2019-03-05
Vue之Element标签页保留用户操作缓存。
2019-03-05