maven搭建ssm项目(入门)

在搭建一个项目的时候,首先考虑基础项目所使用的依赖,这里使用maven管理jar包,在此之前,先把eclipse和maven-3.2.3关联。
maven搭建ssm项目(入门)_第1张图片
导入相关依赖:
为了便于父工程对版本的统一管理,pom文件指定父坐标(这里指出parent-pom的文件内容一般都是固定的,下载点这里, 访问密码 999c),搭建项目直接导入即可,然后,项目pom指明parent坐标,如:
maven搭建ssm项目(入门)_第2张图片
父pom文件中,除了集中定义了依赖的本版号之外,还有
maven搭建ssm项目(入门)_第3张图片
一个简单的ssm需要的jar包如下:spring-context、spring-beans、spring-jdbc、spring-webmvc、spring-aspects;mybatis、mybatis-spring;slf4j、jackson、dbcp、mysql,需要在子工程引入:

        <dependency>
            <groupId>org.mybatisgroupId>
            <artifactId>mybatisartifactId>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-contextartifactId>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-beansartifactId>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-webmvcartifactId>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-jdbcartifactId>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-aspectsartifactId>
        dependency>
        <dependency>
            <groupId>org.mybatisgroupId>
            <artifactId>mybatis-springartifactId>
        dependency>
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
        dependency>
        <dependency>
            <groupId>org.slf4jgroupId>
            <artifactId>slf4j-log4j12artifactId>
        dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.coregroupId>
            <artifactId>jackson-databindartifactId>
        dependency>
        <dependency>
            <groupId>com.jolboxgroupId>
            <artifactId>bonecp-springartifactId>
        dependency>

另外,web项目中都要使用到的还有apache下的:commons-lang、commons-io

        <dependency>
            <groupId>org.apache.commonsgroupId>
            <artifactId>commons-lang3artifactId>
        dependency>
        <dependency>
            <groupId>org.apache.commonsgroupId>
            <artifactId>commons-ioartifactId>
        dependency>

指定maven启动tomcat以及端口:

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.tomcat.mavengroupId>
                <artifactId>tomcat7-maven-pluginartifactId>
                <configuration>
                    <port>80port>
                    <path>/path>
                configuration>
            plugin>
        plugins>
    build>

以上,项目所需要的基本jar包,就算搭建完成了。
运行:点击run-as——>run configurations—–>maven build—–>new—>配置启动
maven搭建ssm项目(入门)_第4张图片

搭建ssm:
jdbc.properties

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true
jdbc.username=root
jdbc.password=root

log4j.properties

log4j.rootLogger=DEBUG,A1
log4j.logger.com.taotao = DEBUG
log4j.logger.org.mybatis = DEBUG
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss,SSS} [%t] [%c]-[%p] %m%n

web.xml


<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
    <display-name>itcast-usermanagedisplay-name>

    <context-param>
        <param-name>contextConfigLocationparam-name>
        <param-value>classpath:spring/applicationContext*.xmlparam-value>
    context-param>

    
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
    listener>

    
    <filter>
        <filter-name>encodingFilterfilter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
        <init-param>
            <param-name>encodingparam-name>
            <param-value>UTF8param-value>
        init-param>
    filter>
    <filter-mapping>
        <filter-name>encodingFilterfilter-name>
        <url-pattern>/*url-pattern>
    filter-mapping>

    
    <servlet>
        <servlet-name>itcast-usermanageservlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
        <init-param>
            <param-name>contextConfigLocationparam-name>
            <param-value>classpath:spring/itcast-usermanage-servlet.xmlparam-value>
        init-param>
        <load-on-startup>1load-on-startup>
    servlet>

    <servlet-mapping>
        <servlet-name>itcast-usermanageservlet-name>
        <url-pattern>/rest/*url-pattern>
    servlet-mapping>

    <welcome-file-list>
        <welcome-file>index.htmlwelcome-file>
    welcome-file-list>

web-app>

application.xml


    <bean  class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        
        <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
        
        <property name="ignoreResourceNotFound" value="true" />
        
        <property name="locations">
            <list>
                <value>classpath:jdbc.propertiesvalue>
            list>
        property>
    bean>

    
    <context:component-scan base-package="cn.itcast"/>

     
    <bean id="dataSource" class="com.jolbox.bonecp.BoneCPDataSource" destroy-method="close">
        
        <property name="driverClass" value="${jdbc.driverClassName}" />
        
        <property name="jdbcUrl" value="${jdbc.url}" />
        
        <property name="username" value="${jdbc.username}" />
        
        <property name="password" value="${jdbc.password}" />
        
        <property name="idleConnectionTestPeriod" value="60" />
        
        <property name="idleMaxAge" value="30" />
        
        
        <property name="maxConnectionsPerPartition" value="100" />
        
        <property name="minConnectionsPerPartition" value="5" />
    bean>


application-transaction.xml:

    
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    bean>

    
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            
            <tx:method name="query*" read-only="true" />
            
            <tx:method name="*" />
        tx:attributes>
    tx:advice>

    <aop:config>
        
        <aop:pointcut id="myPointcut" expression="execution(* cn.itcast.mybatis.service.*.*(..))" />
        
        <aop:advisor advice-ref="txAdvice" pointcut-ref="myPointcut" />
    aop:config>

application-mybatis.xml

    class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource">property>
        <property name="configLocation" value="classpath:mybatis/mybatis-config.xml">property>
        <property name="mapperLocations" value="classpath:mybatis/mappers/**/*.xml">property>
        <property name="typeAliasesPackage" value="cn.itcast.mybatis.pojo">property>
    

    class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="cn.itcast.mybatis.mapper">property>
    

itcast-usermanage-servlet.xml

    <mvc:annotation-driven />
    <context:component-scan base-package="cn.itcast.mybatis.controller" />

    <bean  class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        
        <property name="prefix" value="/WEB-INF/views/" />
        <property name="suffix" value=".jsp" />
    bean>

mybatis-config.xml



<configuration>

    <typeAliases>
        
        <package name="cn.itcast.mybatis.pojo" />
    typeAliases>

configuration>

UserMapper.xml



<mapper namespace="cn.itcast.mybatis.mapper.UserMapper">

<select id="queryUserList" resultType="User">
    SELECT * FROM tb_user
select>

mapper>

UserMapper.java

public interface UserMapper {
    List queryUserList();
}

确定访问一个页面的时候,调用UserMapper的方法,根据id=queryUserList去执行sql语句,返回User,这样,说明整个框架走通:
UserController.java

@RequestMapping("user")
@Controller
public class UserController {
    @Autowired
    private UserService userService;
    @RequestMapping(value="list",method=RequestMethod.GET)
    @ResponseBody
    public EasyUIResult queryList(@RequestParam(value="page",defaultValue="1") Integer page,
            @RequestParam(value="rows",defaultValue="5") Integer rows){
        return this.userService.queryUserList(page,rows);
    }
}

UserService.java

@Service
public class UserService {
    @Autowired
    private UserMapper userMpper;
    public EasyUIResult queryUserList(Integer page, Integer rows) {
        List users=this.userMpper.queryUserList();
        return new EasyUIResult(30L, users);
    }
}

返回结果为EasyUIResult对象,封装了total记录数和 查询到的集合List
EasyUIResult.java

   private Long total;
   private List rows;

访问:http://localhost/rest/user/list?page=1&rows=5

返回:
maven搭建ssm项目(入门)_第5张图片

你可能感兴趣的:(入门项目)