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 ...
随机推荐
- echarts 容器宽度设置百分比,但是图表缩成一团
如图 明明设置了充满整个div,然后发现都缩成了一团,后来发现echarts不能和display:none;属性一起用 解决方法: 把v-show改成v-if就可以了充满容器了...
- 备份是个好习惯 bugku
题目描述: 解题思路: 1.查看网页源码只显示一行字符,有点像16进制,但经过解码并不是,也不是base64等编码 2.根据题目,应该和备份相关,默认页面一般都是 index.php或者index ...
- aspose word导出表格
[HttpGet] [Route("GetPurchaseItemWord")] public IHttpActionResult Get_PurchaseItemWord(str ...
- tomcat的安装以及环境配置
1.Tomcat的下载地址:http://tomcat.apache.org/ Tomcat是开放源代码的WEB服务器,安装时,只需解压压缩包即可 2.环境变量的配置 1>新建系统变量CATAL ...
- CentOS 7.9 环境下部署 Docker 服务
sudo setenforce Permissive sudo vi /etc/selinux/config SELINUX=permissive sudo systemctl stop firewa ...
- 【2020NIO.AC省选模拟#10】C. 寄蒜几盒
题目链接 原题解: 可以发现,假设我们把凸多边形看做障碍,一个点没有被染色当且仅当在它的位置上能看到凸多边形任意两条相对的边中的一条(也就是能看到至少$\dfrac{n}{2}$条边). 对于每个询问 ...
- [2009年NOIP普及组] 分数线划定
世博会志愿者的选拔工作正在A市如火如荼的进行.为了选拔最合适的人才,A市对所有报名的选手进行了笔试,笔试分数达到面试分数线的选手方可进入面试.面试分数线根据计划录取人数的150%划定,即如果计划录取m ...
- C语言所有的数据类型
整型数据类型:char 1 字节 -128 到 127 或 0 到 255unsigned char 1 字节 0 到 255signed char 1 字节 -128 到 127int 2 或 4 ...
- 1.3 选择IDE
选择IDE 使用过的IDE 1.DEV-C++ 配合使用语言:C.C++ 个人体验:支持代码补全,信息竞赛遗留物,招很多入门教材喜欢,十分古老缺乏维护,功能简洁,安装完打开直接编译很方便,代码提示让人 ...
- 20192305 王梓全Python程序设计实验三报告
20192305 王梓全Python程序设计实验三报告 课程:<Python程序设计> 班级: 1923 姓名: 王梓全 学号:20192305 实验教师:王志强 实验日期:2021年5月 ...