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-事务处理

最新文章

  1. Gym 100703G---Game of numbers(DP)
  2. 租房时代,K2 BPM软件带你拥抱更好生活
  3. HTML5跨浏览器表单及HTML5表单的渐进增强
  4. 计算机学院2014年“新生杯”ACM程序设计大赛
  5. POJ3185(简单BFS,主要做测试使用)
  6. poj3083
  7. 【暑假】[实用数据结构]UVAlive 3942 Remember the Word
  8. java中对map使用entrySet循环
  9. perl post json数据
  10. JAVA流读取文件并保存数据
  11. MyBatis(五)select返回list数据
  12. Java爬虫之下载全世界国家的国旗图片
  13. 《剑指offer》二叉树中和为某一值的路径
  14. 彻底理解js中this的指向,不必硬背
  15. 转://执行impdp时出现ORA-39154错误的解决案例
  16. java框架----&gt;quartz的使用(一)
  17. ubuntu编译安装php7, 安装openssl
  18. Android的读写文件及权限设置
  19. postman-3http请求
  20. codevs 1226 倒水问题

热门文章

  1. 【剑指Offer】【链表】链表中倒数第k个结点
  2. Python获取当前在线设备ip和mac地址
  3. ABAP学习(34):cl_gui_alv_grid实现Table Maintain
  4. Mac10.13-10.15 下玩星际争霸1.16
  5. centos/redhat 多路径存储使用 - 客户端
  6. C# 读取Xml文件中的中文
  7. python3+selenium+BeautifulReport生成自动化测试报告
  8. Mysql--查询&quot;01&quot;课程比&quot;02&quot;课程成绩高的学生的信息及课程分数
  9. Optional类与使用==判断null有什么区别?使用Optional类有什么优势?
  10. PTA1002 写出这个数 (20 分)