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

原文地址:https://www.cnblogs.com/soft-test/p/14915141.html