
- jdbc.driver=com.mysql.cj.jdbc.Driver
- jdbc.url=jdbc:mysql://127.0.0.1:3306/xxx?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai
- jdbc.username=root
- jdbc.password=root
1.配置SqlSessionFactory 4. 5.
- <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
- <property name="dataSource" ref="dataSource">property>
-
- <property name="configLocation" value="classpath:mybatis.xml">property>
-
- <property name="mapperLocations" value="classpath:mapper/*.xml">property>
- bean>
2.读取db.properties文件
- <context:property-placeholder location="classpath:db.properties"/>
3.连接数据库
- <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
- <property name="driverClassName" value="${jdbc.driver}">property>
- <property name="url" value="${jdbc.url}">property>
- <property name="username" value="${jdbc.username}">property>
- <property name="password" value="${jdbc.password}">property>
-
- bean>
6.扫描service包
-
- <context:component-scan base-package="com.chen.service">context:component-scan>
7.配置数据源事务管理器
-
- <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
- <property name="dataSource" ref="dataSource">property>
8.配置事务管理管理的方法
9.配置Aop切入点
- "1.0" encoding="UTF-8" ?>
- configuration
- PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
- "http://mybatis.org/dtd/mybatis-3-config.dtd">
- <configuration>
-
- <settings>
-
- <setting name="logImpl" value="STDOUT_LOGGING"/>
- settings>
-
- <typeAliases>
- <package name="com.chen.entity"/>
- typeAliases>
-
- configuration>
- "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"
- xmlns:mvc="http://www.springframework.org/schema/mvc"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- https://www.springframework.org/schema/beans/spring-beans.xsd
- http://www.springframework.org/schema/context
- https://www.springframework.org/schema/context/spring-context.xsd
- http://www.springframework.org/schema/mvc
- https://www.springframework.org/schema/mvc/spring-mvc.xsd">
-
-
- <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
- <property name="prefix" value="/WEB-INF/jsp/">property>
- <property name="suffix" value=".jsp">property>
- bean>
-
-
- <context:component-scan base-package="com.chen.controller"/>
-
- <mvc:annotation-driven conversion-service="conversionService"/>
-
-
- <mvc:view-controller path="/index.html" view-name="login"/>
- <mvc:view-controller path="/login.html" view-name="login"/>
- <mvc:view-controller path="/" view-name="login"/>
- <mvc:view-controller path="/main.html" view-name="main"/>
-
-
-
- <mvc:resources mapping="/asserts/**" location="/asserts/">mvc:resources>
-
-
- <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
- <property name="converters">
- <set>
- <bean class="com.chen.converter.DateConverter" />
- set>
- property>
- bean>
-
- beans>
- "1.0" encoding="UTF-8"?>
- <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
- version="4.0">
-
-
-
- <context-param>
- <param-name>contextConfigLocationparam-name>
- <param-value>classpath:mybatis-spring.xmlparam-value>
- context-param>
-
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
- listener>
-
-
-
-
-
- <servlet>
- <servlet-name>dispatcherServletservlet-name>
- <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
- <init-param>
- <param-name>contextConfigLocationparam-name>
- <param-value>classpath:springmvc.xmlparam-value>
- init-param>
- <load-on-startup>1load-on-startup>
- servlet>
-
- <servlet-mapping>
- <servlet-name>dispatcherServletservlet-name>
- <url-pattern>/url-pattern>
- servlet-mapping>
-
-
- <filter>
- <filter-name>encodingfilter-name>
- <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
-
- <init-param>
- <param-name>encodingparam-name>
- <param-value>UTF-8param-value>
- init-param>
- filter>
- <filter-mapping>
- <filter-name>encodingfilter-name>
- <url-pattern>/*url-pattern>
- filter-mapping>
-
-
- web-app>
注:一个实体类对应一张数据库表,也对应一个usermapper接口和usermapper.xml文件,
一个mapper接口对应一个mapper实现类

user
- package com.chen.entity;
-
- import lombok.Data;
-
- import javax.swing.*;
-
- @Data
- public class User {
- private Integer id;
- private String userName;
- private String pwd;
- }
usermapper接口
- package com.chen.mapper;
-
- import com.chen.entity.User;
- import org.apache.ibatis.annotations.Param;
-
- public interface UserMapper {
- User queryByUserNameAndPwd(@Param("userName") String userName, @Param("pwd") String pwd);
-
- }
UserServiceImpl.java
- package com.chen.service.Impl;
-
- import com.chen.entity.User;
- import com.chen.mapper.UserMapper;
- import com.chen.service.UserService;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
-
- @Service
- public class UserServiceImpl implements UserService {
-
- @Autowired
- private UserMapper userMapper;
-
- @Override
- public boolean login(String userName, String pwd) {
- User user = this.userMapper.queryByUserNameAndPwd(userName, pwd);
- return user != null;
- }
- }
usermapper.xml
- "1.0" encoding="UTF-8" ?>
- mapper
- PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
- "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
- <mapper namespace="com.chen.mapper.UserMapper">
- <select id="queryByUserNameAndPwd" resultType="user">
- SELECT * FROM t_user WHERE user_name=#{userName} and pwd=#{pwd}
- select>
-
- mapper>
controller层负责service层和页面的跳转
UserController对应UserService接口
UserController.java
- package com.chen.controller;
-
- import com.chen.entity.User;
- import com.chen.service.UserService;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.PostMapping;
-
- @Controller
- public class UesrController {
-
- @Autowired
- private UserService userService;
-
- @PostMapping("/user/login")
- public String login(User user) {
-
- System.out.println(user);
-
- boolean result = this.userService.login(user.getUserName(), user.getPwd());
- if (result) {
- return "redirect:/main.html";
- } else {
- return "redirect:/index.html";
- }
- }
-
- }
- package com.chen.converter;
-
- import org.springframework.core.convert.converter.Converter;
-
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- import java.util.Date;
-
- public class DateConverter implements Converter
{ -
- @Override
- public Date convert(String source) {
- try {
- // 把字符串转换为日期类型
- SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
- Date date = simpleDateFormat.parse(source);
-
- return date;
- } catch (ParseException e) {
- e.printStackTrace();
- }
- // 如果转换异常则返回空
- return null;
- }
- }