spring、springmvc和mybatis整合(java config方式)

之前项目中使用ssm框架大多是基于xml的方式,spring3.0以后就提供java config的模式来构建项目,并且也推荐使用这种方式,自从接触过springboot后,深深感受到这种纯java配置的便利,但是springboot默认为我们引入好多jar和配置,使得项目变得很重,因此决定自己动手搭建一个无xml的ssm项目。

开发环境

我的开发环境如下:

web服务器:tomcat8

开发工具:STS

JDK版本:1.8

项目构建工具:maven

搭建过程

1、首先引入相关依赖

pom文件如下:

复制代码

4.0.0
io.powerx
springmvcconfig
war
0.0.1-SNAPSHOT





org.springframework
spring-context
4.3.9.RELEASE


org.springframework
spring-aop
4.3.9.RELEASE


org.springframework
spring-core
4.3.9.RELEASE


org.springframework
spring-beans
4.3.9.RELEASE


org.springframework
spring-jdbc
4.3.9.RELEASE


org.springframework
spring-tx
4.3.9.RELEASE


commons-logging
commons-logging
1.2



org.springframework
spring-web
4.3.9.RELEASE


org.springframework
spring-webmvc
4.3.9.RELEASE

    <!-- servlet配置,不配置也不影响,但是jsp会报错 -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.1.0</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>
    
    <!-- 加载jackson包 -->
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>2.7.3</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.7.3</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-annotations</artifactId>
        <version>2.7.3</version>
    </dependency>
    <!-- druid数据源 -->
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid</artifactId>
        <version>1.0.16</version>
    </dependency>
    <!-- spring和mybatis整合 -->
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis-spring</artifactId>
        <version>1.3.0</version>
    </dependency>

    <!-- MySQL数据库连接驱动 -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.43</version>
    </dependency>
    <!-- MyBatis框架 -->
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis</artifactId>
        <version>3.4.1</version>
    </dependency>
    <!-- 添加logback日志 -->
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.25</version>
    </dependency>
    <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-classic</artifactId>
        <version>1.2.3</version>
    </dependency>
    <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-core</artifactId>
        <version>1.2.3</version>
    </dependency>
</dependencies>
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
    </plugins>
</build>
复制代码

2、编写配置文件

MyWebAppInitializer继承AbstractAnnotationConfigDispatcherServletInitializer,并重写其中的方法(另外一种方式是MyWebAppInitializer直接实现WebApplicationInitializer接口,不过会复杂一点),它是我们程序的入口,web容器启动后会调其中相关的方法从而启动整个应用。主要实现3个方法:getRootConfigClasses,负责加载spring容器,本例中我们只加载了RootConfig类,其它一些spring相关配置类也可以在这里加载;getServletConfigClasses,加载springmvc容器,本例中我们加载了MvcConfig;getServletMappings,设置映射的路径。配置代码如下:

MyWebAppInitializer.java

复制代码
package powerx.io.config;

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

public class MyWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

protected Class<?>[] getRootConfigClasses() {
    return new Class<?>[]{RootConfig.class};
}

protected Class<?>[] getServletConfigClasses() {
    return new Class<?>[]{MvcConfig.class};
}

protected String[] getServletMappings() {
    return new String[]{"/"};
}

}
复制代码
  RootConfig.java

复制代码
package powerx.io.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.context.annotation.Import;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

@Configuration
@EnableTransactionManagement //开启事务支持
@Import(DruidDataSourceConfig.class)//导入数据源的配置
@ComponentScan(basePackages = {“powerx.io.service”,“powerx.io.dao”,“powerx.io.config”},
excludeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION, value = EnableWebMvc.class)})
public class RootConfig {
}
复制代码
  MvcConfig.java

复制代码
package powerx.io.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

@Configuration
@EnableWebMvc
@ComponentScan(“powerx.io.controller”)
public class MvcConfig extends WebMvcConfigurerAdapter{

@Override
public void addViewControllers(ViewControllerRegistry registry) {
    registry.addViewController("/").setViewName("index");
    super.addViewControllers(registry);
}

//配置jsp视图
@Bean
public ViewResolver viewResolver(){
    InternalResourceViewResolver resolver = new InternalResourceViewResolver();
    resolver.setPrefix("/WEB-INF/");
    resolver.setSuffix(".jsp");
    resolver.setExposeContextBeansAsAttributes(true);
    return resolver;
}

//配置静态资源处理
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
    configurer.enable();//将静态资源的请求转发到servlet容器中默认的servlet上
}

}
复制代码
  DruidDataSourceConfig.java

复制代码
package powerx.io.config;

import java.io.IOException;
import java.sql.SQLException;

import javax.sql.DataSource;

import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;

import com.alibaba.druid.pool.DruidDataSource;

@Configuration
@PropertySource(“classpath:jdbc.properties”)
@MapperScan(basePackages=“powerx.io.dao”)
public class DruidDataSourceConfig{

@Value("${spring.datasource.url}")  
private String dbUrl;  
  
@Value("${spring.datasource.username}")  
private String username;  
  
@Value("${spring.datasource.password}")  
private String password;  
  
@Value("${spring.datasource.driverClassName}")  
private String driverClassName;  
  
@Value("${spring.datasource.initialSize}")  
private int initialSize;  
  
@Value("${spring.datasource.minIdle}")  
private int minIdle;  
  
@Value("${spring.datasource.maxActive}")  
private int maxActive;  
  
@Value("${spring.datasource.maxWait}")  
private int maxWait;  
  
@Value("${spring.datasource.timeBetweenEvictionRunsMillis}")  
private int timeBetweenEvictionRunsMillis;  
  
@Value("${spring.datasource.minEvictableIdleTimeMillis}")  
private int minEvictableIdleTimeMillis;  
  
@Value("${spring.datasource.validationQuery}")  
private String validationQuery;  
  
@Value("${spring.datasource.testWhileIdle}")  
private boolean testWhileIdle;  
  
@Value("${spring.datasource.testOnBorrow}")  
private boolean testOnBorrow;  
  
@Value("${spring.datasource.testOnReturn}")  
private boolean testOnReturn;  
  
@Value("${spring.datasource.poolPreparedStatements}")  
private boolean poolPreparedStatements;  
  
@Value("${spring.datasource.maxPoolPreparedStatementPerConnectionSize}")  
private int maxPoolPreparedStatementPerConnectionSize;  
  
@Value("${spring.datasource.filters}")  
private String filters;  
  
@Value("{spring.datasource.connectionProperties}")  
private String connectionProperties;  
  
@Bean     //声明其为Bean实例  
public DataSource dataSource(){
    DruidDataSource datasource = new DruidDataSource();  
      
    datasource.setUrl(this.dbUrl);  
    datasource.setUsername(username);  
    datasource.setPassword(password);  
    datasource.setDriverClassName(driverClassName);  
    datasource.setInitialSize(initialSize);  
    datasource.setMinIdle(minIdle);  
    datasource.setMaxActive(maxActive);  
    datasource.setMaxWait(maxWait);  
    datasource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);  
    datasource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);  
    datasource.setValidationQuery(validationQuery);  
    datasource.setTestWhileIdle(testWhileIdle);  
    datasource.setTestOnBorrow(testOnBorrow);  
    datasource.setTestOnReturn(testOnReturn);  
    datasource.setPoolPreparedStatements(poolPreparedStatements);  
    datasource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize);  
    try {  
        datasource.setFilters(filters);  
    } catch (SQLException e) {  
    }  
    datasource.setConnectionProperties(connectionProperties);  
    return datasource;  
}

//mybatis的配置
@Bean
public SqlSessionFactoryBean sqlSessionFactoryBean() throws IOException{
    ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();  
    SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();//mybatis-plus插件类
    sqlSessionFactoryBean.setDataSource(dataSource());//数据源
    sqlSessionFactoryBean.setMapperLocations(resourcePatternResolver.getResources("classpath:mappers/*.xml"));
    sqlSessionFactoryBean.setTypeAliasesPackage("powerx.io.model");//别名,让*Mpper.xml实体类映射可以不加上具体包名
    return sqlSessionFactoryBean;
}

}
复制代码
  3、配置数据源和mapper映射文件等

jdbc.properties

复制代码
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/testssm
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.initialSize=5
spring.datasource.minIdle=5
spring.datasource.maxActive=20
spring.datasource.maxWait=60000
spring.datasource.timeBetweenEvictionRunsMillis=60000
spring.datasource.minEvictableIdleTimeMillis=300000
spring.datasource.validationQuery=SELECT 1 FROM DUAL
spring.datasource.testWhileIdle=true
spring.datasource.testOnBorrow=false
spring.datasource.testOnReturn=false
spring.datasource.poolPreparedStatements=true
spring.datasource.maxPoolPreparedStatementPerConnectionSize=20
spring.datasource.filters=stat
spring.datasource.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
复制代码
  UserMapper.xml

复制代码

<?xml version="1.0" encoding="UTF-8" ?>
<select id="findByName" parameterType="String" resultMap="UserResult">
    SELECT USER_NAME,USER_PWD FROM USER WHERE USER_NAME=#{userName} 
</select>

<insert id="insertUser" parameterType="powerx.io.model.User">
INSERT INTO USER(USER_NAME,USER_PWD) VALUES(#{userName},#{userPwd})
复制代码   User.java

复制代码
package powerx.io.model;

public class User {

private String userName;
private String userPwd;
public String getUserName() {
    return userName;
}
public void setUserName(String userName) {
    this.userName = userName;
}
public String getUserPwd() {
    return userPwd;
}
public void setUserPwd(String userPwd) {
    this.userPwd = userPwd;
}

}
复制代码
  UserDao.java

复制代码
package powerx.io.dao;

import powerx.io.model.User;

public interface UserDao {

User findByName(String userName);

int insertUser(User user);

}
复制代码
4、控制层和service层

UserService.java

复制代码
package powerx.io.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import powerx.io.dao.UserDao;
import powerx.io.model.User;

@Service
public class UserService {

@Autowired
private UserDao userDao;
public Object findByName(String userName) {
    return userDao.findByName(userName);
 }

@Transactional
public int insertUser(User user) {
    //测试事务是否起作用
    userDao.insertUser(user);
    return userDao.insertUser(user);
 }

}
复制代码
  UserContoller.java

复制代码
package powerx.io.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.ResponseBody;

import powerx.io.model.User;
import powerx.io.service.UserService;

@Controller
public class UserContoller {

public static Logger logger = LoggerFactory.getLogger("monitor");
@Autowired
private UserService userService;

@ResponseBody
@RequestMapping("/get")
public Object getData(String userName) {
    logger.info("获取用户信息");
    return userService.findByName(userName);
}

@ResponseBody
@RequestMapping("/insert")
public Object insert(User user) {
    logger.info("添加用户");
    return userService.insertUser(user);
}

}
复制代码
5、项目结构图

在这里插入图片描述

总结

写完代码,在往tomcat中部署运行的时候遇到了一个问题,tomcat启动失败,报了一大堆异常,其中一个是java.util.zip.ZipException: invalid LOC header (bad signature),从网上找了一大堆答案,基本肯定是依赖的jar包不完整造成的,但是我搜索了一下maven仓库,删除了所有in_progress的包,依然不行,无奈只能一个一个的排除,浪费了好久时间,最后查出原因是因为mysql的包没有下载完整,但是依然是一个.jar结尾的文件,只不过大小不对,所以在此记录一下,以后在遇到tomcat启动失败问题,一定要细心排查所依赖的jar。

至此,整个ssm框架搭建完毕,在其中我加入了事务控制和日志管理,基本上可以作为一个生产项目的基础demo。项目代码基本上都以贴上,我也上传到了码云上面,方便自己使用,地址:

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值