案例
导入架包
<!-- https://mvnrepository.com/artifact/org.springframework/spring-beans --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>5.1.5.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-context --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.1.5.RELEASE</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.16</version> </dependency> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.2</version> </dependency> <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver --> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.9.2</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.1.5.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.38</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.6</version> </dependency> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.2</version> </dependency> <!--引入需要的ehcache插件--> <dependency> <groupId>net.sf.ehcache</groupId> <artifactId>ehcache</artifactId> <version>1.2.3</version> </dependency> <!--mybatis整合ehcache的jar--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-ehcache</artifactId> <version>1.0.0</version> </dependency> <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.3.2</version> </dependency> <!-- https://mvnrepository.com/artifact/com.alibaba/druid --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.10</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.25</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.25</version> <scope>test</scope> </dependency> <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.47</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.1.5.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>5.1.5.RELEASE</version> </dependency> <dependency> <groupId>javaee</groupId> <artifactId>javaee-api</artifactId> <version>5</version> </dependency> <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.9.8</version> </dependency> <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.9.8</version> </dependency> <!-- https://mvnrepository.com/artifact/commons-io/commons-io --> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.4</version> </dependency> <!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload --> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.1</version> </dependency> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency>
applicationContext.xml配置文件
<!--扫描注解--> <context:component-scan base-package="com.ssm"/> <!--添加MVC支持--> <mvc:annotation-driven/> <!--配置视图解析器--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/"/> <property name="suffix" value=".jsp"/> </bean> <!--释放静态资源--> <mvc:default-servlet-handler/> <!--加载配置文件--> <context:property-placeholder location="classpath:jdbc.properties"/> <!--配置数据源--> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"> <property name="driverClassName" value="${jdbc.driver}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </bean> <!--SqlSessionFactory--> <bean class="org.mybatis.spring.SqlSessionFactoryBean"> <!--加载数据源--> <property name="dataSource" ref="dataSource"/> <!--加载Mybatis配置文件--> <property name="configLocation" value="classpath:mybatis-config.xml"/> </bean> <!--扫描Dao层--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.ssm.dao"/> </bean> <!--开启事务注解支持--> <tx:annotation-driven/> <!--事务管理器--> <bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean>
jdbc.properties文件
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/smbms?useUniCode=true&characterEncoding=utf-8 jdbc.username=root jdbc.password=123
log4j.properties文件
### direct log messages to stdout ### log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.Target=System.out log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n ### direct messages to file mylog.log ### log4j.appender.file=org.apache.log4j.FileAppender log4j.appender.file.File=:\ss\ssm\mylog.log log4j.appender.file.layout=org.apache.log4j.PatternLayout log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n ### set log levels - for more verbose logging change 'info' to 'debug' ### log4j.rootLogger=debug, stdout,file ###log4j.logger.com.smbms.dao=debug, stdout,file
mybatis-config.xml文件
<!--根节点--> <configuration> <settings> <!--log4j配置引入--> <setting name="logImpl" value="LOG4J"/> </settings> <!--设置别名--> <typeAliases> <!--全局设置别名:默认以类名作为别名--> <package name="com.ssm.entity"/> </typeAliases> <!--mappers 代表小配置集合--> <mappers> <!--<mapper resource="org/mybatis/example/BlogMapper.xml"/>--> <package name="com.ssm.dao"/> </mappers> </configuration>
web.xml文件
<web-app> <display-name>Archetype Created Web Application</display-name> <filter> <filter-name>characterEncodingFilter</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> <!--强制使用UTF-8编码--> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>characterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!--中央调度器--> <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:applicationContext.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>
entity包下Smbms_User类
public class Smbms_User implements Serializable {
private Integer id;
private String usercode;
private String username;
private String userpassword;
private Integer gender;
private Date birthday;
private String phone;
private String address;
private Integer userrole;
private Integer createdby;
private Date creationdate;
private Integer modifyby;
private Date modifydate;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsercode() {
return usercode;
}
public void setUsercode(String usercode) {
this.usercode = usercode == null ? null : usercode.trim();
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username == null ? null : username.trim();
}
public String getUserpassword() {
return userpassword;
}
public void setUserpassword(String userpassword) {
this.userpassword = userpassword == null ? null : userpassword.trim();
}
public Integer getGender() {
return gender;
}
public void setGender(Integer gender) {
this.gender = gender;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone == null ? null : phone.trim();
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address == null ? null : address.trim();
}
public Integer getUserrole() {
return userrole;
}
public void setUserrole(Integer userrole) {
this.userrole = userrole;
}
public Integer getCreatedby() {
return createdby;
}
public void setCreatedby(Integer createdby) {
this.createdby = createdby;
}
public Date getCreationdate() {
return creationdate;
}
public void setCreationdate(Date creationdate) {
this.creationdate = creationdate;
}
public Integer getModifyby() {
return modifyby;
}
public void setModifyby(Integer modifyby) {
this.modifyby = modifyby;
}
public Date getModifydate() {
return modifydate;
}
public void setModifydate(Date modifydate) {
this.modifydate = modifydate;
}
}
dao层包下Smbms_UserMapper接口
@Repository public interface Smbms_UserMapper { /*int deleteByPrimaryKey(Long id); int insert(Smbms_User record); int insertSelective(Smbms_User record); Smbms_User selectByPrimaryKey(Long id); int updateByPrimaryKeySelective(Smbms_User record); int updateByPrimaryKey(Smbms_User record);*/ @Select("select * from smbms_user where userCode=#{user_code} and userPassword=#{user_password}") public Smbms_User login(@Param("user_code") String user_code, @Param("user_password") String user_password); }
service层包下Smbms_UserService接口
public interface Smbms_UserService { public Smbms_User login(String user_code, String user_password); }
serviceimpl层包下的Smbms_UserServcieImpl类
@Service("smbms_UserService") public class Smbms_UserServcieImpl implements Smbms_UserService { //植入Dao层对象 @Resource private Smbms_UserMapper smbms_userMapper; //事务 /* @Transactional(isolation=Isolation.DEFAULT)*/ @Override public Smbms_User login(String user_code, String user_password) { return smbms_userMapper.login(user_code,user_password); } }
controller包下的Smbms_UserController类
@Controller @RequestMapping("/user") public class Smbms_UserController { //植入Service层分对象 @Resource private Smbms_UserService smbms_userService; @RequestMapping("/login") public String doLogin(Smbms_User smbms_user, HttpSession session){ Smbms_User user = smbms_userService.login(smbms_user.getUsercode(), smbms_user.getUserpassword()); if(user!=null){ session.setAttribute("user",user); return "welcome"; } return "login"; } }
login.jsp页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>登陆</title> </head> <body> <div> <form action="/user/login" method="post"> 用户名:<input type="text" name="usercode"/> 密码:<input type="password" name="userpassword"/> <input type="submit" value="登陆"/> </form> </div> </body> </html>
welcome.jsp页面
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %> <html> <head> <title>欢迎</title> </head> <body> 欢迎光临:${user.username} </body> </html>
过滤器拦截案例
@WebFilter(urlPatterns = {"/*"}) public class LoginFilter implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException { } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest servletRequest=(HttpServletRequest) request; if (servletRequest.getRequestURI().equals("/user/login")){ chain.doFilter(request,response); } Object user =servletRequest.getSession().getAttribute("user"); if (user==null){ servletRequest.getRequestDispatcher("login.jsp").forward(request,response); }else { chain.doFilter(request,response); } } @Override public void destroy() { } }