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 ...
随机推荐
- iOS开发 网络学习(4)HTTPS
一.HTTPS简介 HTTPS : Hyper Text Transfer Protocol over Secure Socket Layer,是以安全为目标的HTTP通道,简单讲是HTTP的安全版. ...
- 请求/响应拦截器 给请求添加token认证
- CSS3滤镜属性filter让网页变黑白
很多特殊的时候,我们向英雄们致敬,在互联网上最常见的方式就是整个网页变黑白,今天逛某博客收集一段代码,用于网页整体变黑白,用css3滤镜属性filter让网页马上变黑白,一行代码就搞定. 在你的css ...
- 【otter搭建】在Linux下搭建阿里开源otter数据同步平台
一,准备安装包和配置文件 JDK:https://www.oracle.com/technetwork/java/javase/downloads/index.html Zookeeper:https ...
- Ubuntu tmux使用教程
sudo apt-get install tmux 安装tmux tmux new -s session_name 新开一个会话 tmux a -t session_name 查看指定会话 tmux ...
- Git配置新学
Git中的AutoCRLF与SafeCRLF换行符问题 https://zhuanlan.zhihu.com/p/380574688 https://xiaozhuanlan.com/topic/40 ...
- STM32自学笔记
1位带操作 第一种位带操作 #define BITBAND_REG(Reg,Bit) (*((uint32_t volatile*)(0x42000000u + (((uint32_t)&(R ...
- bzoj 3603
考虑转化问题:一个点相邻元素中有偶数个$1$等价于一个点与相邻元素异或和为$0$ 于是直接列出异或方程组求解即可 注意由于要求不允许出现全0矩阵,因此如果有自由元直接给成$1$ 贴代码: #inclu ...
- 项目实训 DAY 10
今天,我写了一些前端代码规范,并按规范修改了一下代码.规范写到了README.md上
- 关于在Eclipse中使用EclEmma
在LAB2中,要求使用EclEmma来统计JUnit测试用例的代码覆盖度.下面就来说说如何进行基本的使用来应付实验(bushi). 在这给出完全体,可以访问下面的网址进行学习. http://www. ...