@EnableTransactionManagement //开启事务注解
启动必要:
package com.zaz; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; @EnableTransactionManagement //开启事务注解 @SpringBootApplication public class SpringBoot_application extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) { return builder.sources(com.zaz.SpringBoot_application.class); } public static void main(String[] args) { SpringApplication.run(com.zaz.SpringBoot_application.class,args); } }pom.xml
banner.txtSpring_Boot_01 com.zaz 1.0-SNAPSHOT 4.0.0 war spring-boot-starter-parent org.springframework.boot 2.5.5 org.springframework.boot spring-boot-starter-web2.4.2 org.springframework.boot spring-boot-starter-thymeleaf2.4.2 org.springframework.boot spring-boot-starter-tomcat2.4.2 org.springframework.boot spring-boot-starter-jdbc2.4.2 org.springframework.boot spring-boot-starter-aop2.4.2 aopalliance aopalliance1.0 org.aspectj aspectjweaver1.9.5 org.mybatis.spring.boot mybatis-spring-boot-starter2.2.0 com.oracle.database.jdbc ojdbc821.3.0.0 com.mchange c3p00.9.5.5 com.mchange mchange-commons-java0.2.20 com.github.pagehelper pagehelper5.2.0 log4j log4j1.2.17 junit junitRELEASE test junit junitRELEASE test src/main/java ***.xml false src/main/resources **dao/mapper/common/*.xml #读取映射文件
_ _ _ _ / / | / / / / / / / / // / | / _ _ _ // /// | / / / // | / / / / ____/ / / ///....// / / /.....// / ./// / /.//... ------- /./...//.// | | ------------ / // |-------| ------ / /// // | | -------- // | | ----- // / |-------| | | / GGGGGGG GGGGGGG | | | | | GG V GG | | | | | /// GGGGGGG GGGGGG GG . GG | | | | | /// GG V GGGGG .. GG | | | / | / /// GG .. GG GG G / /// GG G GGG GGG /// GG GG GGGGGG /// GGG GGG / GGGGGG ; / / / | / // | // // -------------| /// .Love | ----------- | ../ / / . | | | / / / / / / / / / . .. Love -----|----- / / / / / . | / / / // / ..Love --------|---- / / /// / / | / / / / --------- // // /// / | | / / / | | / / --------- / / / / _ _ / / / ___ / / / / / / / | | / / / / --- -- - -- - / / / / -- -- ------ - ------- --- / / / / You're so good at it. [Chinese: nì hài]log4j.properties
log4j.rootLogger=DEBUG,CONSOLE,file #log4j.rootLogger=ERROR,ROLLING_FILE log4j.logger.com.zte.*.dao=debug log4j.logger.com.ibatis=debug log4j.logger.com.ibatis.common.jdbc.SimpleDataSource=debug log4j.logger.com.ibatis.common.jdbc.scriptRunner=debug log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate=debug log4j.logger.java.sql.Connection=debug log4j.logger.java.sql.Statement=debug log4j.logger.java.sql.PreparedStatement=debug log4j.logger.java.sql.ResultSet=debug log4j.logger.org.tuckey.web.filters.urlrewrite.UrlRewriteFilter=debug ###################################################################################### # Console Appender u65e5u5fd7u5728u63a7u5236u8f93u51fau914du7f6e ###################################################################################### log4j.appender.ConSOLE=org.apache.log4j.ConsoleAppender log4j.appender.Threshold=error log4j.appender.CONSOLE.Target=System.out log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout log4j.appender.CONSOLE.layout.ConversionPattern= [%p] %d %c - %m%n ###################################################################################### # DailyRolling File u6bcfu5929u4ea7u751fu4e00u4e2au65e5u5fd7u6587u4ef6uff0cu6587u4ef6u540du683cu5f0f:log2009-09-11 ###################################################################################### log4j.appender.file=org.apache.log4j.DailyRollingFileAppender log4j.appender.file.DatePattern=yyyy-MM-dd log4j.appender.file.File=log.log log4j.appender.file.Append=true log4j.appender.file.Threshold=error log4j.appender.file.layout=org.apache.log4j.PatternLayout log4j.appender.file.layout.ConversionPattern=%d{yyyy-M-d HH:mm:ss}%x[%5p](%F:%L) %m%n log4j.logger.com.opensymphony.xwork2=errormybatis-config.xml
C3P0Config
配置c3p0配置信息:
package com.zaz.config; import com.mchange.v2.c3p0.ComboPooledDataSource; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import javax.sql.DataSource; @Configuration public class C3P0Config { @Bean(name = "dataSource") @ConfigurationProperties(prefix = "spring.datasource") public DataSource createDataSource(){ return new ComboPooledDataSource(); } }MvcConfig
mvc的配置文件,拦截器配置:
package com.zaz.config; import com.zaz.interceptor.LoginInterceptor; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @Configuration public class MvcConfig implements WebMvcConfigurer { @Override public void addInterceptors(InterceptorRegistry registry) { // registry.addInterceptor(new LoginInterceptor()).addPathPatterns("/**").excludePathPatterns("/static/**","/user/userList"); } }LoginInterceptor
package com.zaz.interceptor; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; public class LoginInterceptor implements HandlerInterceptor { public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { //拦截器 HttpSession session = request.getSession(); Object username = session.getAttribute("username"); //判断地址是否为登录 if(username == null){ //判断如果为AJAX请求则返回登录页面 并让请求为错误 if("XMLHttpRequest".equals(request.getHeader("X-Requested-With"))){ response.setHeader("Content-Type","text/plain;charset=utf8"); response.getWriter().write("FAILURE"); }else { response.sendRedirect(request.getContextPath()+"/login.jsp"); } return false; }else { return true; } } }文件树
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-iJYS8A6c-1635084902635)(C:/Users/lenovo/AppData/Roaming/Typora/typora-user-images/image-20211018173643290.png)]
UserActionpackage com.zaz.user.control; import com.zaz.user.model.SmbmsUser; import com.zaz.user.service.IUserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import javax.servlet.http.HttpSession; import java.util.ArrayList; import java.util.List; @Controller @RequestMapping("/user") public class UserAction { @Autowired private IUserService iUserService; @RequestMapping("/mainView") public String mainView(){ return "maina"; } @RequestMapping("/maininfo1") public String mainView1(SmbmsUser smbmsUser, Model model, HttpSession session){ System.out.println(smbmsUser); SmbmsUser s = new SmbmsUser(); s.setUsername("张三"); model.addAttribute("user",s); session.setAttribute("username",s); return "maina"; } @RequestMapping("/maininfo2") @ResponseBody public ListIUserDaomainView2(SmbmsUser smbmsUser, Model model, HttpSession session){ System.out.println(smbmsUser); SmbmsUser s = new SmbmsUser(); s.setUsername("张三"); model.addAttribute("user",s); session.setAttribute("username",s); List list = new ArrayList<>(); return list; } @RequestMapping("/zhanshi") @ResponseBody public List zhanshi(SmbmsUser smbmsUser, Model model, HttpSession session){ SmbmsUser s = new SmbmsUser(); List userListService = iUserService.getUserListService(s); System.out.println(userListService); return userListService; } }
package com.zaz.user.dao; import com.zaz.user.model.SmbmsUser; import org.apache.ibatis.annotations.Mapper; import java.util.List; @Mapper //mapper接口 public interface IUserDao { public ListUserServicegetUserList(SmbmsUser smbmsUser); }
package com.zaz.user.service.impl; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import com.zaz.user.dao.IUserDao; import com.zaz.user.model.SmbmsUser; import com.zaz.user.service.IUserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.util.List; @Transactional @Service public class UserService implements IUserService { @Autowired private IUserDao iUserDao; @Override public Listlogin.htmlgetUserListService(SmbmsUser smbmsUser) { return iUserDao.getUserList(smbmsUser); } @Transactional public PageInfo getUserListServicePage(SmbmsUser smbmsUser, PageInfo pageInfo) { PageHelper.startPage(pageInfo.getPageNum(),pageInfo.getPageSize()); List proList = iUserDao.getUserList(smbmsUser); PageInfo pageInfo1 = new PageInfo (proList); return pageInfo1; } }
maina.html登录 登录页面 主页面
Title 主页面啊
Title 主页面啊欢迎分享,转载请注明来源:内存溢出
评论列表(0条)