用Spring MVC和MyBatis实现登录和注册功能的代码示例
时间: 2023-11-27 07:53:46 浏览: 90
以下是用Spring MVC和MyBatis实现登录和注册功能的示例代码:
1. 配置web.xml
```
<web-app>
<display-name>SpringMVC+MyBatis Demo</display-name>
<servlet>
<servlet-name>SpringMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/springmvc-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>/index.jsp</welcome-file>
</welcome-file-list>
</web-app>
```
2. 配置springmvc-config.xml
```
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="com.example.controller" />
<mvc:annotation-driven />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/views/"
p:suffix=".jsp" />
</beans>
```
3. 配置数据库连接
```
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/test" />
<property name="username" value="root" />
<property name="password" value="123456" />
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:mybatis-config.xml" />
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.example.mapper" />
</bean>
```
4. 编写Mapper接口和Mapper映射文件
UserMapper.java
```
public interface UserMapper {
User getUserByName(String name);
void addUser(User user);
}
```
UserMapper.xml
```
<mapper namespace="com.example.mapper.UserMapper">
<select id="getUserByName" parameterType="java.lang.String" resultType="com.example.model.User">
SELECT * FROM user WHERE name=#{name}
</select>
<insert id="addUser" parameterType="com.example.model.User">
INSERT INTO user(name, password) VALUES(#{name}, #{password})
</insert>
</mapper>
```
5. 编写Controller
```
@Controller
public class UserController {
@Autowired
private UserMapper userMapper;
@RequestMapping(value="/login", method=RequestMethod.POST)
public ModelAndView login(@RequestParam("name") String name,
@RequestParam("password") String password) {
User user = userMapper.getUserByName(name);
if(user != null && user.getPassword().equals(password)) {
return new ModelAndView("success");
} else {
return new ModelAndView("error");
}
}
@RequestMapping(value="/register", method=RequestMethod.POST)
public ModelAndView register(@RequestParam("name") String name,
@RequestParam("password") String password) {
User user = userMapper.getUserByName(name);
if(user != null) {
return new ModelAndView("error");
} else {
user = new User();
user.setName(name);
user.setPassword(password);
userMapper.addUser(user);
return new ModelAndView("success");
}
}
}
```
6. 编写JSP页面
login.jsp
```
<form action="login" method="post">
<label for="name">Name:</label>
<input type="text" id="name" name="name"><br><br>
<label for="password">Password:</label>
<input type="password" id="password" name="password"><br><br>
<input type="submit" value="Submit">
</form>
```
register.jsp
```
<form action="register" method="post">
<label for="name">Name:</label>
<input type="text" id="name" name="name"><br><br>
<label for="password">Password:</label>
<input type="password" id="password" name="password"><br><br>
<input type="submit" value="Submit">
</form>
```
success.jsp
```
<h2>Success!</h2>
```
error.jsp
```
<h2>Error!</h2>
```
以上就是用Spring MVC和MyBatis实现登录和注册功能的示例代码。
阅读全文