手记

SSM+maven整合步骤

SSM框架是由SpringMVC、Spring、MyBatis框架整合而成.

maven

pom.xml配置

<dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>

    <!--配置oracle-->
    <dependency>
      <groupId>com.oracle</groupId>
      <artifactId>ojdbc6</artifactId>
      <version>11.2.0.1.0</version>
    </dependency>

    <!--spring相关配置-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-beans</artifactId>
      <version>4.2.8.RELEASE</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>4.2.8.RELEASE</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>4.2.8.RELEASE</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>4.2.8.RELEASE</version>
    </dependency>


    <dependency><!--与mybatis交互的jdbc-->
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>4.2.8.RELEASE</version>
    </dependency>


    <!--mybatis的jar包-->
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>3.2.3</version>
    </dependency>

    <!--spring和mybatis交互的连接jar包-->
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis-spring</artifactId>
      <version>1.3.1</version>
    </dependency>

  </dependencies>

SpringMVC:

web.xml配置文件

<!--配置springMVC-->
  <servlet>
    <servlet-name>springMVC</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!--解决配置文件-->
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath*:spring/springMVC-*.xml</param-value>
    </init-param>
    <!--早点被加载-->
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>springMVC</servlet-name>
    <url-pattern>*.action</url-pattern>
  </servlet-mapping>

SpringMVC-servlet.xml配置

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd">
    
    <!--扫描带@Control的类-->
    <context:component-scan base-package="com.zr.actions"/></beans>

Spring配置

web.xml配置

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:spring/applicationContext.xml</param-value>
  </context-param>

  <!--spring-->
  <!--监听在容器启动的时候被调用,容器关闭的时候消失spring的配置监听器监听对象HttpServletRequest、HttpSession、ServletContext-->
  <listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

applicationContext.xml配置

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd">
    <context:component-scan base-package="com.zr">
        <!--regex:正则表达式 扫描不包含actions包-->
        <context:exclude-filter type="regex" expression="com.zr.actions"/>
    </context:component-scan></beans>

MyBatis配置

applicationContext.xml配置

 <!--数据源-->
    <bean id="dataSource" class="org.apache.ibatis.datasource.pooled.PooledDataSource">
        <property name="username" value = "spring"></property>
        <property name="password" value="root"></property>
        <property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl"></property>
        <property name="driver" value="oracle.jdbc.OracleDriver"></property>
        <property name="poolMaximumActiveConnections" value="20"></property>
        <!--最大等待时间-->
        <property name="poolTimeToWait" value="2000"></property>
    </bean>


    <bean id="sqlSessionFactory" class = "org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <property name="typeAliasesPackage"  value="com.zr.entity"></property>
        <!--mapper文件和Mapper.xml不在同一路径下需要-->
        <property name="mapperLocations" value="classpath:mybatis/*.Mapper.xml"></property>
    </bean>

    <!--将接口和mapper.xml对应起来,自动注入-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
        <property name="basePackage" value="com.zr.mapper"></property>
 </bean>

接口及相关类的配置

user类

//别名@Alias("user")public class User {    private Integer id;    private String userName;    private String passWord;    public Integer getId() {        return id;
    }    public void setId(Integer id) {        this.id = id;
    }    public String getUserName() {        return userName;
    }    public void setUserName(String userName) {        this.userName = userName;
    }    public String getPassWord() {        return passWord;
    }    public void setPassWord(String passWord) {        this.passWord = passWord;
    }    @Override
    public String toString() {        return "User{" +                "id=" + id +                ", userName='" + userName + '\'' +                ", passWord='" + passWord + '\'' +                '}';
    }
}

IUserMapper接口

public interface IUserMapper {    User findByUserAndPass(
            @Param("userName") String userName,
            @Param("passWord") String passWord);
}

IUserService接口

public interface IUserService {    boolean loginCheck(String userName,String passWord);

}

UserServiceImpl类(service的实现类)

@Service("ius")public class UserServiceImpl implements IUserService {    @Resource
    private IUserMapper iUserMapper;    @Override
    public boolean loginCheck(String userName, String passWord) {
        User user = iUserMapper.findByUserAndPass(userName,passWord);        if(user==null){            return false;
        }else{            return true;
        }
    }
}



作者:_借东西的小人
链接:https://www.jianshu.com/p/4d76ae2aa9ef


1人推荐
随时随地看视频
慕课网APP