基于Spring+Mybatis+jsp+servlet的用户登录注册功能(数据库查找和插入用户),jsp写的前端(css+javascript),使用IDEA
!!!代码放在最后
网页前端的设计:jsp编写,使用css美化(字体使用Google font,图标使用font awemome,颜色使用palettes|flat ui colors,谷歌可找到,个人觉得挺好用)
使用了简单的js进行表单验证用户名长度、密码、确认密码
项目结构
(1)建立一个名为spring的数据库,user_information的表,字段id、username、password
(2)前端jsp、css代码编写
(3)sqlMapConfig.xml的Mybatis总配置文件
(4)ApplicationContext.xml的Spring总配置文件
(5)编写User类 bean
(6)UserMapper接口和UserMapper.xml定义实现SQL语句findById、findByName、findByUser、insertUser方法
(7)UserService接口和UserServiceImpl实现类(实现addAccount和hasAccount方法)
(8)util工具包下的servlet类(LoginServlet和RegisterServlet)
配置文件:
(1)sqlMapConfig.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<typeAliases>
<package name="com.dingxiang.bean"/>
</typeAliases>
<mappers>
<mapper class="com.dingxiang.mapper.UserMapper"/>
</mappers>
</configuration>
(2)UserMapper.xml(配置UserMapper接口实现)
<?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.dingxiang.mapper.UserMapper">
<select id="findById" resultType="User" parameterType="Integer">
select * from spring.user_information where id=#{id}
</select>
<select id="findByName" parameterType="String" resultType="User">
select * from spring.user_information where username=#{username}
</select>
<select id="findByUser" parameterType="User" resultType="User">
select * from spring.user_information where username=#{username} and password=#{password}
</select>
<insert id="insertUser" parameterType="User">
insert into spring.user_information (username,password) values(#{username},#{password})
</insert>
</mapper>
(3)ApplicationContext.xml(配置DataSource、sqlsessionFactory、Mapper实现把User交给Spring管理,数据库实现Mybatis由Spring帮我们完成)
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframe