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 ...
随机推荐
- leetcode 27. 移除元素 【时间击败100.00%】【内存击败84.67%】
1 public int removeElement(int[] nums, int val) { 2 int last = nums.length - 1; 3 for (int i = 0; i ...
- OSP6部署流程
准备4台虚拟机,完成初始化 一.架构如下: Controller 控制节点 也可以复用为计算节点 192.168.6.11 Compute01 192.168.6.21 Compute02 ...
- MVC对session或cookie保存的值在js中做处理
在cshtml中保存数据 eg: @Html.Hidden("sessionUserName", Session["userName"]) @Html.Hidd ...
- mybatis纵览
Mybatis MyBatis 是一款优秀的持久层框架,它支持自定义 SQL.存储过程以及高级映射.MyBatis 免除了几乎所有的 JDBC 代码以及设置参数和获取结果集的工作. MyBatis 可 ...
- SQL 用 in 大于 1000 问题解决
-- 今天生成环境数据突然多,系统异常 解决方案(必须用in 业务情况),也可以用其他函数解决 union all 或者 exists 等 1:截取list List<Integer> ...
- Typora怎么让左边的标题折叠
点击文件选择偏好设置->在选择外观->选中侧边栏的大纲视图允许折叠和展开 效果
- 2020第十一届蓝桥杯大赛软件类国赛题目 C/C++ B 组
试题 A: 美丽的 2 本题总分:5 分 问题描述:在公元 1 年到公元 2020 年(包含)中,有多少个年份的数位中包含数字 2? #include <stdio.h> #include ...
- 【APT】海莲花组织DLL样本分析
前言 样本来源Twitter,之前的文章已经完整分析过一个类似的DLL样本,这次做个简单记录. 样本分析 样本信息如下: DLL文件共有40个导出函数: 导出函数内容基本一致,恶意代码都在DllMai ...
- 获取客户端ip,请求头伪造ip,解决办法
可以在请求头加入 X-Forwarder-For 来伪造访问的ip地址 //Nginx支持X-Forwarded-For 配置 proxy_set_header X-Forwarded-For $pr ...
- C#下解析、生成JAVA的RSA密钥、公钥
1.从 https://www.nuget.org/packages/BouncyCastle/下载对应的nupkg包,放到本地一个文件夹中 2.打开VS2010,工具->NuGet程序包管理器 ...