项目结构
数据表结构
pom文件
4.0.0
org.example
MavenWeb3
1.0-SNAPSHOT
war
javax.servlet
javax.servlet-api
3.0.1
provided
commons-io
commons-io
2.4
org.projectlombok
lombok
1.18.12
org.mybatis
mybatis
3.5.6
mysql
mysql-connector-java
5.1.8
org.apache.tomcat.maven
tomcat7-maven-plugin
2.2
8080
/MavenWeb3
pojo
@Data//包括get、set、equals、hashCode、canEqual、toString、无参构造,没有有参构造
@NoArgsConstructor
public class User {
private Integer id;
private String name;
private String password;
private String email;
private Date birthday;
private String infoId;
private String englishTeacher;
public User(Integer id, String name, String password, String email, Date birthday, String infoId, String englishTeacher) {
this.id = id;
this.name = name;
this.password = password;
this.email = email;
this.birthday = birthday;
this.infoId = infoId;
this.englishTeacher = englishTeacher;
}
}
mybatis核心配置mybatis-config.xml
mapper接口
public interface UserMapper {
@Select("select * from users where name = #{name} and password = #{password}")
User selectByNameAndPwd(@Param("name")String name,@Param("password")String password);
@Select("select * from users where name = #{name}")
User selectByName(@Param("name") String name);
@Insert("insert into users(name,password,email,birthday,infoId,english_teacher)" +
" values(#{name},#{password},#{email},#{birthday},#{infoId},#{englishTeacher});")
int insert(User user);
}
sql映射文件UserMapper.xml
登录界面html
login
注册界面html
register
登录的servlet
@WebServlet("/loginServlet")
public class LoginServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("UTF-8");
String name = req.getParameter("name");
String password = req.getParameter("password");
SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtils.getSqlSessionFactory();
//2.获取SqlSession对象,用它来执行sql
SqlSession sqlSession = sqlSessionFactory.openSession();
//3.执行sql
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
User user = mapper.selectByNameAndPwd(name,password);
resp.setCharacterEncoding("utf-8");
resp.setContentType("text/html;charset=utf-8");
PrintWriter writer = resp.getWriter();
if (user != null){
writer.write("login success!");
}else{
writer.write("login fail!");
}
//4.释放资源
sqlSession.close();
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
注册的servlet
@WebServlet("/registerServlet")
public class RegisterServlet extends HttpServlet {
@SneakyThrows
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("UTF-8");
String name = req.getParameter("name");
String password = req.getParameter("password");
String email = req.getParameter("email");
String birthday = req.getParameter("birthday");
String infoId = req.getParameter("infoId");
String englishTeacher = req.getParameter("englishTeacher");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date birthdayDate = sdf.parse(birthday);
User user = new User(null, name, password, email, birthdayDate, infoId, englishTeacher);
//1.加载mybatis核心配置,获取SqlSessionFactory
SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtils.getSqlSessionFactory();
//2.获取SqlSession对象,用它来执行sql
SqlSession sqlSession = sqlSessionFactory.openSession();
//3.执行sql
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
User existUserName = mapper.selectByName(name);
resp.setCharacterEncoding("utf-8");
resp.setContentType("text/html;charset=utf-8");
PrintWriter writer = resp.getWriter();
if (existUserName ==null){
int count = mapper.insert(user);
sqlSession.commit();
//4.释放资源
sqlSession.close();
writer.write("注册成功");
}else{
writer.write("用户名太受欢迎了");
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req,resp);
}
}
SqlSessionFactoryUtils
/**
* 通过静态代码块使得该类再被JVM加载时仅执行唯一一次静态代码块中的内容,生成一个sqlSessionFactory对象保存在该类变量中
* 后续直接通过public的getSqlSessionFactory方法拿到这个对象即可。避免重复频繁创建sqlSessionFactory对象的问题。
*/
public class SqlSessionFactoryUtils {
private static SqlSessionFactory sqlSessionFactory;
static {
try {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
public static SqlSessionFactory getSqlSessionFactory(){
return sqlSessionFactory;
}
}