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

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 }

复制代码

entity包中的类

复制代码

1 package com.oukele.entity;

2

3 public class User {

4 private String userName;

5 private String password;

6

7 public String getUserName() {

8 return userName;

9 }

10

11 public void setUserName(String userName) {

12 this.userName = userName;

13 }

14

15 public String getPassword() {

16 return password;

17 }

18

19 public void setPassword(String password) {

20 this.password = password;

21 }

22

23 @Override

24 public String toString() {

25 return “User{” +

26 “userName='” + userName + ‘’’ +

27 “, password='” + password + ‘’’ +

28 ‘}’;

29 }

30 }

复制代码

servlet包中的类

复制代码

1 package com.oukele.servlet;

2

3 import com.oukele.dao.UserMapper;

4 import com.oukele.entity.User;

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

6 import org.springframework.stereotype.Service;

7

8 @Service

9 public class UserServlet {

10

11 @Autowired

12 private UserMapper userMapper;

13

14 public Boolean checkUser(User user){

15 //根据传进来的参数,去数据库查找用户

16 int result = userMapper.check(user);

17 //如果查回来的结果不为零,说明数据库中有该用户,让他登陆,否则不让他登陆

18 if( result > 0 ){

19 return true;

20 }

21 return false;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值