使用IDEA搭建一个Spring + AOP (权限管理 ) + Spring MVC + Mybatis的Web项目 (零配置文件)(1)

使用IDEA搭建一个Spring + AOP (权限管理 ) + Spring MVC + Mybatis的Web项目 (零配置文件)

=========================================================================================================================

前言:

===

除了mybatis 不是零配置,有些还是有xml的配置文件在里面的。

注解是Spring的一个构建的一个重要手段,减少写配置文件,下面解释一下一些要用到的注解:

@Configuration 作用于类上面,声明当前类是一个配置类(相当于一个Spring的xml文件)

@ComponentScan(“xxx”) 作用于类上面,自动扫描xxx包名下所有使用@Service、@Component、@Repository和@Controller的类,并注册为Bean

@Bean 作用与类和方法上,相当于Spring配置文件bean节点

@EnableWebMvc 作用于类,开启一些默认配置,如一些ViewResolver或者MessageConverter

@RequestMapping 作用于类、方法,配置URL和方法之间的映射

@RequestBody 作用于参数前,允许request的参数在request体中,而不是在直接链接在地址后面

@ResponseBody 作用于返回值、方法上,支持将返回值放在response体中,而不是返回一个页面。

@RequestParam 作用于参数前,将form的对应name值映射到当前参数中。

这里我就不从一开始了。

pom.xml

+ View Code

项目结构:

先把上面的包,依次创建好,(webapp文件夹中的WEB-INF所有文件全部删掉)

我们从 config (配置) 包开始吧。

RootConfig类 (类似于spring-root.xml配置文件一样)

复制代码

1 package com.oukele.config.spring;

2

3 import org.springframework.context.annotation.ComponentScan;

4 import org.springframework.context.annotation.Configuration;

5 import org.springframework.context.annotation.Import;

6 import org.springframework.web.servlet.config.annotation.EnableWebMvc;

7

8 @Configuration//声明当前类 是一个 配置类

9 @ComponentScan(basePackages = “com.oukele.dao”)//扫描 dao包 使用spring 注解的接口

10 @Import({SpringDaoConfig.class,SpringServlet.class})//导入其他配置

11 public class RootConfig {

12

13 }

复制代码

SpringWebConfig类 (相当于 spring-web.xml )

复制代码

package com.oukele.config.mvc;

import org.springframework.context.annotation.ComponentScan;

import org.springframework.context.annotation.Configuration;

import org.springframework.context.annotation.EnableAspectJAutoProxy;

import org.springframework.web.servlet.config.annotation.EnableWebMvc;

import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;

import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration//声明当前类是配置类 类似于 spring-web.xml 文件

@EnableWebMvc// 若无此注解 WebMvcConfigurer 无效

@ComponentScan(basePackages = {“com.oukele.controller”,“com.oukele.aspect”})//扫描控制层 和切面 包

@EnableAspectJAutoProxy// 激活 切面 代理

public class SpringWebConfig implements WebMvcConfigurer {

//注册静态资源,没注册的话,网站是访问不了的

@Override

public void addResourceHandlers(ResourceHandlerRegistry registry) {

registry.addResourceHandler(“/css/**”).addResourceLocations(“/WEB-INF/css/”);

}

}

复制代码

SpringDaoConfig类 (  配置 数据源  )

复制代码

1 package com.oukele.config.spring;

2

3 import com.mchange.v2.c3p0.ComboPooledDataSource;

4 import org.mybatis.spring.SqlSessionFactoryBean;

5 import org.mybatis.spring.mapper.MapperScannerConfigurer;

6 import org.springframework.beans.factory.annotation.Autowired;

7 import org.springframework.context.EnvironmentAware;

8 import org.springframework.context.annotation.Bean;

9 import org.springframework.context.annotation.Configuration;

10 import org.springframework.context.annotation.PropertySource;

11 import org.springframework.core.env.Environment;

12 import org.springframework.core.io.ClassPathResource;

13 import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

14

15 import javax.sql.DataSource;

16 import java.beans.PropertyVetoException;

17 import java.io.IOException;

18

19 @Configuration

20 @PropertySource(value = “classpath:jdbc.properties”)//加载资源文件

21 public class SpringDaoConfig implements EnvironmentAware {//数据层

22

23 @Autowired

24 private Environment env;

25

26 @Override

27 public void setEnvironment(Environment environment) {

28 this.env = environment;

29 }

30

31 //配置数据源

32 @Bean

33 public DataSource dataSource() throws PropertyVetoException {

34 ComboPooledDataSource dataSource = new ComboPooledDataSource();

35 dataSource.setDriverClass(env.getProperty(“jdbc.driver”));

36 dataSource.setJdbcUrl(env.getProperty(“jdbc.url”));

37 dataSource.setUser(env.getProperty(“jdbc.user”));

38 dataSource.setPassword(env.getProperty(“jdbc.password”));

39 return dataSource;

40 }

41 //配置 mybatis

42 @Bean

43 public SqlSessionFactoryBean sqlSessionFactoryBean() throws IOException, PropertyVetoException {

44 SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();

45 PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();

46 factoryBean.setTypeAliasesPackage(“com.oukele.entity”);//实体类

47 factoryBean.setMapperLocations(resolver.getResources(“classpath:mapper/*.xml”));// 映射 sql文件

48 factoryBean.setConfigLocation(new ClassPathResource(“mybatis-config.xml”));//mybatis配置

49 factoryBean.setDataSource(dataSource());

50 return factoryBean;

51 }

52 // 简化调用 将 dao 放置 容器中 使用:比如

53 //@Autowired

54 //xxxMapper xxmaper ;

55 //也可以在类上面 直接使用 @ComponentScan 扫描接口

56 @Bean

57 public MapperScannerConfigurer mapperScannerConfigurer() {

58 MapperScannerConfigurer configurer = new MapperScannerConfigurer();

59 configurer.setBasePackage(“com.oukele.dao”);//扫描接口

60 configurer.setSqlSessionFactoryBeanName(“sqlSessionFactoryBean”);

61 return configurer;

62 }

63

64 }

复制代码

SpringServlet 类 (扫描 )

复制代码

1 package com.oukele.config.spring;

2

3 import org.springframework.context.annotation.ComponentScan;

4 import org.springframework.context.annotation.Configuration;

5

6 @Configuration

7 @ComponentScan(basePackages = “com.oukele.servlet”)

8 public class SpringServlet {//服务层

9

10 }

复制代码

WebConfig 类 (类似于 web.xml文件)

复制代码

1 package com.oukele.config.web;

2

3 import com.oukele.config.mvc.SpringWebConfig;

4 import com.oukele.config.spring.RootConfig;

5 import org.springframework.web.filter.CharacterEncodingFilter;

6 import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

7

8 import javax.servlet.Filter;

9

10 public class WebConfig extends AbstractAnnotationConfigDispatcherServletInitializer {

11

12 @Override

13 protected Class<?>[] getRootConfigClasses() {// (spring容器) 父容器

14 return new Class[]{RootConfig.class};

15 }

16

17 @Override

18 protected Class<?>[] getServletConfigClasses() {// (spring mvc容器) 子容器

19 return new Class[]{SpringWebConfig.class};

20 }

21

22 @Override

23 protected String[] getServletMappings() {//映射

24 return new String[]{“/”};

25 }

26

27 //设置编码 这里设置好像没有用。。。。。 有解决方案请告诉我,谢谢

28 @Override

29 protected Filter[] getServletFilters() {

30 CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();

31 characterEncodingFilter.setEncoding(“UTF-8”);

32 return new Filter[]{characterEncodingFilter};

33 }

34 }

复制代码

Controller 包 里面的类

复制代码

1 package com.oukele.controller;

2

3 import com.oukele.entity.User;

4 import com.oukele.servlet.UserServlet;

5 import org.springframework.beans.factory.annotation.Autowired;

6 import org.springframework.web.bind.annotation.*;

7 import org.springframework.web.context.WebApplicationContext;

8

9 import javax.servlet.http.HttpSession;

10 import java.util.HashMap;

11 import java.util.Objects;

12

13 @RestController

14 public class LoginController {

15

16 @Autowired

17 private WebApplicationContext webApplicationContext;

18

19 @Autowired

20 private UserServlet userServlet;

21

22 @RequestMapping(path = “/login/{name}/{password}”, method = RequestMethod.GET,produces = “application/json;charset=utf-8”)

23 public String login(@PathVariable(“name”) String name, @PathVariable(“password”) String password, HttpSession session) {

24 User user = new User();

25 user.setUserName(name);

26 user.setPassword(password);

27 session.setAttribute(“username”,user);//存入session中

28 Boolean aBoolean = userServlet.checkUser(user);

29

30 if(aBoolean){

31 return “{“msg”:“登入成功”}”;

32 }

33

34 return “{“msg”:“登入失败”}”;

35 }

36

37 @GetMapping(path = “/Ioc”)

38 public HashMap<String, String[]> getAllInfo() {

39

40 return new HashMap<String, String[]>() {{

41 put(“子容器”, webApplicationContext.getBeanDefinitionNames());

42 put(“父容器”, Objects.requireNonNull(webApplicationContext.getParent().getBeanDefinitionNames()));

43 }};

44

45 }

46

47 }

复制代码

dao包中的接口

复制代码

1 package com.oukele.dao;

2

3 import com.oukele.entity.User;

4 import org.apache.ibatis.annotations.Select;

5 import org.springframework.stereotype.Repository;

6

7 @Repository

8 public interface UserMapper {

9

10 //使用xml配置文件

11 int check(User user);

12 //使用注解

13 @Select(“select count(*) from user where userName = #{userName} and password = #{password}”)

14 int check1(User user);

15

16 }

最后

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助。

因此收集整理了一份《2024年Web前端开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点!不论你是刚入门Android开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门!

如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助。

因此收集整理了一份《2024年Web前端开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

[外链图片转存中…(img-bgq8BkQP-1715827180488)]

[外链图片转存中…(img-booew7VL-1715827180488)]

[外链图片转存中…(img-6lSQut8s-1715827180489)]

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点!不论你是刚入门Android开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门!

如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值