前台登录页面(导入pom.xml)
index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<body>
<h2>各位好</h2>
<form action="/login" method="post">
<table>
<h2>中音管理系统</h2>
<tr>
<td>账号:</td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="password" name="password"></td>
</tr>
<tr>
<td><button type="submit">登录</button></td>
</tr>
</table>
</form>
</body>
</html>
需要配置web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<display-name>a</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<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:/springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<filter>
<filter-name>encoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
src进行建包
springmvc.xml进行数据库连接,然后配置属性
<?xml version="1.0" encoding="UTF-8"?>
<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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-mvc.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-mvc.xsd">
<context:annotation-config/>
<!-- 开启注解 -->
<context:component-scan base-package="com.hp"/>
<!-- 启动mvc的驱动 -->
<mvc:annotation-driven/>
<!-- 静态资源处理 -->
<mvc:default-servlet-handler/>
<!-- 数据库连接 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/asd?serverTimezone=Asia/Shanghai"/>
<property name="username" value="root"></property>
<property name="password" value="123456"></property>
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="mapperLocations" value="classpath:com/hp/mapper/Mapper.xml"></property>
<property name="dataSource" ref="dataSource"></property>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.hp.dao"></property>
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
</bean>
</beans>
UserDao
package com.hp.dao;
import java.util.List;
import java.util.Map;
public interface UserDao {
//登录
Map<String,Object> login(Map<String, Object> map);
//查所有
List<Map<String, Object>> queryAll();
}
Mapper.xml
<?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.hp.dao.UserDao">
<select id="login" parameterType="map" resultType="map">
select * from emp where name =#{name} and password=#{password}
</select>
<select id="queryAll" parameterType="map" resultType="map">
select e.id,e.name,e.password,e.birthday,e.sex,d.name AS dname from emp e LEFT JOIN dept d ON d.id=e.did
</select>
</mapper>
UserService
package com.hp.service;
import java.util.List;
import java.util.Map;
public interface UserService {
Map<String,Object> login(Map<String, Object> map);
List<Map<String, Object>> queryAll();
}
UserServiceimpl
package com.hp.service;
import com.hp.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;
@Service
public class UserServiceimpl implements UserService{
@Autowired
UserDao userDao;
@Override
public Map<String, Object> login(Map<String, Object> map){
return userDao.login(map);
}
@Override
public List<Map<String, Object>> queryAll() {
return userDao.queryAll();
}
}
UserController
package com.hp.controller;
import com.hp.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
@Controller
public class UserController {
@Autowired
UserService userService;
@RequestMapping(value = "/login")
public ModelAndView login(@RequestParam Map<String, Object> map){
ModelAndView mv=new ModelAndView();
map=userService.login(map);
if(map!=null){
mv.setViewName("redirect:/queryAll");
}
return mv;
}
@RequestMapping(value = "/queryAll")
public ModelAndView queryAll(){
ModelAndView mv=new ModelAndView("/queryAll.jsp");
List<Map<String, Object>> list=userService.queryAll();
mv.addObject("list",list);
return mv;
}
}