SSM登录注册
已经好久没有用过SSM了,就用一个经典的登录注册来复习一下把.我觉得想要对走得更远SSM还是不能丢的.
搭建项目
首先项目如果要跑起来需要在web服务器上运行,我肯定用经典的Tomcat呀. tomcat运行的时候需要一个部署包,maven项目执行install后会生成一个target目录,这个项目下的文件就是部署包. tomcat在选择模块后也会生成部署包.在不配置的情况下,部署包中的所见即所得,除了META-INF和WEB-INF下的文件被拦截,其他文件都可以访问到,默认情况下会把index.jsp作为主页.
添加和配置spring mvc
在pom.xml中添加依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
在web.xml中添加servlet配置
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-*.xml</param-value>
<!--classpath:表示在resources目录下 spring-*.xml表示配置文件为所有以spring开头的-的xml文件-->
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
在resources中添加spring-mvc.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"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://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">
<!-- 设置扫描包-->
<context:component-scan base-package="com.my"/>
<!-- 允许注解-->
<mvc:annotation-driven/>
<!-- 设置jsp前缀后缀-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
至此就可以访问到相关的Request请求了
整合mybatis
添加相关依赖
spring和mybatis 要版本对应 http://mybatis.org/spring/
druid用最新的应该就可以
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>${mybatis.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>${druid.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring-version}</version>
</dependency>
添加application.properties配置mysql相关
druid.driverClassName=com.mysql.cj.jdbc.Driver
druid.url=jdbc:mysql://127.0.0.1:3306/testdb?serverTimeZone=UTC&useUnicode=true&characterEncoding=UTF-8
druid.username=root
druid.password=root
druid.initialSize=10
druid.minIdle=6
druid.maxActive=50
druid.maxWait=60000
添加spring-dao.xml配置mybatis相关
<?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 https://www.springframework.org/schema/context/spring-context.xsd">
<context:property-placeholder location="classpath:application.properties"/>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<property name="url" value="${druid.url}"/>
<property name="username" value="${druid.username}"/>
<property name="password" value="${druid.password}"/>
<property name="driverClassName" value="${druid.driverClassName}"/>
<!-- 初始化连接数量 -->
<property name="initialSize" value="${druid.initialSize}"/>
<!-- 最小空闲连接数 -->
<property name="minIdle" value="${druid.minIdle}"/>
<!-- 最大并发连接数 -->
<property name="maxActive" value="${druid.maxActive}"/>
<!-- 配置获取连接等待超时的时间 -->
<property name="maxWait" value="${druid.maxWait}"/>
</bean>
<!--3 会话工厂bean sqlSessionFactoryBean -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 数据源 -->
<property name="dataSource" ref="dataSource"/>
<!-- 别名 -->
<property name="typeAliasesPackage" value="com.my.entity"/>
<!-- sql映射文件路径 -->
<property name="mapperLocations" value="classpath*:mapper/*.xml"/>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--指定会话工厂,如果当前上下文中只定义了一个则该属性可省去 -->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<!-- 指定要自动扫描接口的基础包,实现接口 -->
<property name="basePackage" value="com.my.dao"/>
</bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 使用注解事务,需要添加Transactional注解属性 -->
<tx:annotation-driven />
</beans>
**配置完之后mybatis应该就算整合完成了. 在mapperLocations中配置的路径里添加Mapper文件 在basePackage配置的路径中添加接口可以使用@Transactional注解管理事物 **
开发接口
相关mapper文件中设置namespace,id与dao层的方法名相同
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.my.dao.LoginMapper">
<select id="getUserByName" parameterType="string" resultType="com.my.entity.User">
select *
from user
where username = #{username,jdbcType=VARCHAR}
</select>
<insert id="addUser" keyColumn="id" keyProperty="id" parameterType="com.my.entity.User" useGeneratedKeys="true">
insert into user (username, `password`)
values (#{username,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR})
</insert>
</mapper>
@Repository
public interface LoginMapper {
/**
* 通过用户名获取账号
*
* @param username 用户名
* @return 账号实体
*/
User getUserByName(String username);
/**
* 添加用户
*
* @param user 用户实体
* @return 失败为0成功为1 返回值可设为Boolean值
*/
int addUser(User user);
}
Service添加@Transactional注解
*/
@Service
@Transactional(rollbackFor = Exception.class)
public interface LoginService
实现接口后在spring-mvc.xml中添加bean
<bean class="com.my.service.impl.LoginServiceImpl"/>
用**@Autowired**注解调用注入的方法
在Controller层添加**@RequestMapping**添加接口
@RequestMapping("/login")
public String login(String username,String password)
实现完成之后就可以访问到接口了,在jsp页面的表单中添加请求
<form action="/login" >
因为就一个登录注册,拦截器和过滤器之类的就没有添加.