06-Spring整合mybatis实现简易登录
1. 文件结构

pojo-Users:
//属性名与数据库列名一致
public class Users implements Serializable {
private int uid;
private String username;
private String password;
private String city;
getter()/setter()
toString()
}
mapper(dao层)-UsersMapper:
@Repository
public interface UsersMapper {
@Select("select * from users where username = #{param1} and password = #{param2}")
public Users login(String username,String password);
}
service-UsersService接口:
public interface UsersService {
public Users login(String username,String password);
}
service-impl-UsersServiceImpl实现类:
@Service
public class UsersServiceImpl implements UsersService {
@Autowired
UsersMapper usersMapper;
@Override
public Users login(String username, String password) {
return usersMapper.login(username,password);
}
}
数据库配置文件db.properties:
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/mybatisdata?serverTimezone=CST&characterEncoding=utf-8
name=root
password=123456
log4j.properties(略)
在spring中配置mybatis连接数据库-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" 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"> <!-- bean definitions here -->
<!-- 引入数据源配置文件-->
<context:property-placeholder location="classpath:db.properties"></context:property-placeholder>
<!-- mybatis配置数据源-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="url" value="${url}"></property>
<property name="username" value="${name}"></property>
<property name="password" value="${password}"></property>
<property name="driverClassName" value="${driver}"></property>
</bean>
<!-- 创建SqlSessionFactoryBean对象,交由spring管理-->
<bean class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 扫描mapper文件-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.yd.mapper"></property>
</bean>
</beans>
配置spring.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"> <!-- bean definitions here -->
<!-- spring需要扫描service层-->
<context:component-scan base-package="com.yd.service"></context:component-scan>
</beans>
测试代码:
public class Demo {
public static void main(String[] args) {
//引入xml配置文件
ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("mybatis.xml", "spring.xml");
UsersServiceImpl userServiceImpl = (UsersServiceImpl) ac.getBean("usersServiceImpl");
Users users = userServiceImpl.login("小丽", "131313");
if (users == null){
System.out.println("用户名或密码输入有误!");
}else{
System.out.println("登录成功!");
}
}
}
运行结果:

不含事务转账与登录业务类似(略)--转账事务参见下一节07-事务处理
06-Spring整合mybatis实现简易登录的更多相关文章
- 【springboot spring mybatis】看我怎么将springboot与spring整合mybatis与druid数据源
目录 概述 1.mybatis 2.druid 壹:spring整合 2.jdbc.properties 3.mybatis-config.xml 二:java代码 1.mapper 2.servic ...
- Spring整合MyBatis小结
MyBatis在Spring中的配置 我们在Spring中写项目需要运用到数据库时,现在一般用的是MyBatis的框架来帮助我们书写代码,但是学习了SSM就要知道M指的就是MyBatis,在此,在Sp ...
- Spring学习总结(六)——Spring整合MyBatis完整示例
为了梳理前面学习的内容<Spring整合MyBatis(Maven+MySQL)一>与<Spring整合MyBatis(Maven+MySQL)二>,做一个完整的示例完成一个简 ...
- Spring学习总结(五)——Spring整合MyBatis(Maven+MySQL)二
接着上一篇博客<Spring整合MyBatis(Maven+MySQL)一>继续. Spring的开放性和扩张性在J2EE应用领域得到了充分的证明,与其他优秀框架无缝的集成是Spring最 ...
- 分析下为什么spring 整合mybatis后为啥用不上session缓存
因为一直用spring整合了mybatis,所以很少用到mybatis的session缓存. 习惯是本地缓存自己用map写或者引入第三方的本地缓存框架ehcache,Guava 所以提出来纠结下 实验 ...
- 2017年2月16日 分析下为什么spring 整合mybatis后为啥用不上session缓存
因为一直用spring整合了mybatis,所以很少用到mybatis的session缓存. 习惯是本地缓存自己用map写或者引入第三方的本地缓存框架ehcache,Guava 所以提出来纠结下 实验 ...
- spring整合mybatis错误:class path resource [config/spring/springmvc.xml] cannot be opened because it does not exist
spring 整合Mybatis 运行环境:jdk1.7.0_17+tomcat 7 + spring:3.2.0 +mybatis:3.2.7+ eclipse 错误:class path reso ...
- spring 整合Mybatis 《报错集合,总结更新》
错误:java.lang.NoClassDefFoundError: org/aspectj/weaver/reflect/ReflectionWorld$ReflectionWorldExcepti ...
- spring整合mybatis(hibernate)配置
一.Spring整合配置Mybatis spring整合mybatis可以不需要mybatis-config.xml配置文件,直接通过spring配置文件一步到位.一般需要具备如下几个基本配置. 1. ...
- spring 整合 mybatis 中数据源的几种配置方式
因为spring 整合mybatis的过程中, 有好几种整合方式,尤其是数据源那块,经常看到不一样的配置方式,总感觉有点乱,所以今天有空总结下. 一.采用org.mybatis.spring.mapp ...
随机推荐
- go 镜像输出一个树 mac模式
递归写的很low,mac模式 package main import "fmt" type TreeNode struct { Value int Left *TreeNode r ...
- ubuntu22.04安装mysql5.7
22版本的ubuntu默认安装mysql8.0版本,要想安装5.x的版本就得下载安装,这个比较详细的教程,可以参考: https://www.cnblogs.com/juanxincai/p/1648 ...
- hdfs操作——hdfs的shell命令和hdfs的JavaAPI操作
hdfs解决hadoop海量数据的存储. shell 命令(所有hadoop fs 可由 hdfs dfs代替) (1) 在hdfs上创建目录 hadoop fs -mkdir 目录名 (2) 本地文 ...
- 模态框:JavaScript+css
solution one: JavaScript,单个模态框展示: modal_tools.js window.onload = function () { //js默认加载页面方法 // get m ...
- kibana启动时报错:Validation Failed: 1: this action would add [2] total shards, but this cluster currently has [999]/[1000] maximum shards open
解决方案: curl -XPUT -H "Content-Type:application/json" -d '{"persistent":{"clu ...
- 20211306 实验一《Python程序设计》实验报告
202111306 丁文博 第一次实验报告 课程:<Python程序设计> 班级: 2113 姓名: 丁文博 学号:20211306 实验教师:王志强 实验日期:2022年3月24日 必修 ...
- POJ--1852-c++实现
因为蚂蚁的朝向不明确,所以,可以根据需要假定朝向方向 首先,当每只蚂蚁朝着离自己最近的端点前进,且不回头则,所需总时间最少 当每只蚂蚁朝着离自己最远的端点前进,所需时间最多,在这期间,会碰到其他蚂蚁, ...
- 1903021126 申文骏 Java 第七周作业 客户类测试
项目 内容 课程班级博客链接 19级信计班(本) 作业要求链接 Java 第七周作业 博客名称 1903021126 申文骏 Java 第七周作业 客户类测试 要求 每道题要有题目,代码(使 ...
- Jetpack compose学习笔记之列表(布局)
一,简介 Jetpack compose中的布局主要分为Column,Row,Box. 二,Column创建的列表 Column创建list时,不管内容是在屏幕内还是屏幕外,都会将list的内容全部创 ...
- Python+Django(4)——创建其他网页(模板继承)
模板继承: 1,修改主页 父模板:抽取通用元素,在index.html同级目录下新建base.html <p> <a href="{% url 'learning_logs ...