写基于ssm框架注册登录的代码
时间: 2023-09-28 12:13:21 浏览: 115
好的,我来为你写一份基于SSM框架实现注册登录的代码。
首先,我们需要在pom.xml文件中添加相关依赖:
```xml
<dependencies>
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.5</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.5</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.3.5</version>
</dependency>
<!-- MyBatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.6</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.6</version>
</dependency>
<!-- 数据库驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.23</version>
</dependency>
<!-- JSP & Servlet -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.3</version>
<scope>provided</scope>
</dependency>
<!-- JSTL -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
```
接下来,我们需要在web.xml文件中配置Spring的前端控制器DispatcherServlet:
```xml
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
```
在src/main/resources目录下创建spring-servlet.xml文件,配置Spring相关的配置信息和控制器:
```xml
<!-- 配置数据源 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/test?useSSL=false&serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</bean>
<!-- 配置MyBatis -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="typeAliasesPackage" value="com.example.model"/>
</bean>
<!-- 配置Mapper扫描 -->
<mybatis-spring:scan base-package="com.example.dao"/>
<!-- 配置事务 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 配置SpringMVC -->
<context:component-scan base-package="com.example"/>
<mvc:annotation-driven/>
<!-- 配置视图解析器 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
```
我们还需要在src/main/webapp/WEB-INF/views目录下创建register.jsp和login.jsp页面,分别用于用户注册和登录。
接下来,我们编写控制器和DAO接口的实现类。
UserController.java:
```java
@Controller
public class UserController {
@Autowired
private UserService userService;
@RequestMapping(value = "/register", method = RequestMethod.GET)
public String showRegisterPage(Model model) {
model.addAttribute("user", new User());
return "register";
}
@RequestMapping(value = "/register", method = RequestMethod.POST)
public String registerUser(@ModelAttribute("user") User user, BindingResult result, Model model) {
if (result.hasErrors()) {
return "register";
}
if (userService.isUserNameExist(user.getUserName())) {
model.addAttribute("error", "用户名已存在");
return "register";
}
userService.addUser(user);
return "redirect:/login";
}
@RequestMapping(value = "/login", method = RequestMethod.GET)
public String showLoginPage(Model model) {
model.addAttribute("user", new User());
return "login";
}
@RequestMapping(value = "/login", method = RequestMethod.POST)
public String loginUser(@ModelAttribute("user") User user, BindingResult result, HttpSession session, Model model) {
if (result.hasErrors()) {
return "login";
}
if (!userService.isUserNameExist(user.getUserName())) {
model.addAttribute("error", "用户名不存在");
return "login";
}
User dbUser = userService.getUserByUserName(user.getUserName());
if (dbUser.getPassword().equals(user.getPassword())) {
session.setAttribute("user", dbUser);
return "redirect:/home";
} else {
model.addAttribute("error", "密码错误");
return "login";
}
}
@RequestMapping(value = "/logout", method = RequestMethod.GET)
public String logoutUser(HttpSession session) {
session.invalidate();
return "redirect:/login";
}
}
```
UserService.java:
```java
@Service
@Transactional
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public boolean isUserNameExist(String userName) {
User user = userMapper.selectByUserName(userName);
return user != null;
}
@Override
public void addUser(User user) {
userMapper.insert(user);
}
@Override
public User getUserByUserName(String userName) {
return userMapper.selectByUserName(userName);
}
}
```
UserMapper.java:
```java
public interface UserMapper {
void insert(User user);
User selectByUserName(String userName);
}
```
User.java:
```java
public class User {
private int id;
private String userName;
private String password;
private String email;
// getter & setter methods
}
```
最后,我们需要在src/main/resources目录下创建mybatis-config.xml文件,配置MyBatis的相关信息:
```xml
<configuration>
<settings>
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
</configuration>
```
好了,以上就是基于SSM框架实现注册登录的代码。
阅读全文