idea使用SSM搭建简单的web项目实现CURD

idea使用SSM搭建简单的web项目实现CURD

      • 1、创建工程,并添加工程配置Tomcat
      • 2、利用Maven配置pom.xml导入需要的依赖
      • 3、创建数据库中的表以及实体类;
        • 3.1、MySQL数据库创建表
        • 3.2、对应数据库创建实体类
      • 4、 dao层(创建接口)
      • 5、 根据dao创建配置对应的mapper文件(xml)
      • 6、配置applicationContext.xml
      • 7、 编写对应的service接口及实现类
      • 8、 控制器Controller
          • 1、配置web.xml
          • 2、配置spring-mvc.xml
      • 9、 junit测试(及早测试,即时测试,及时测试)

注:理解图
idea使用SSM搭建简单的web项目实现CURD_第1张图片
idea使用SSM搭建简单的web项目实现CURD_第2张图片

1、创建工程,并添加工程配置Tomcat

2、利用Maven配置pom.xml导入需要的依赖

  • pom.xml代码示例:


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0modelVersion>

  <groupId>com.某某某groupId>
  <artifactId>springSpringMVCMyBatis01artifactId>
  <version>1.0version>
  <packaging>warpackaging>

  <name>springSpringMVCMyBatis01 Maven Webappname>
  
  <url>http://www.example.comurl>

  <properties>
    <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
    <maven.compiler.source>1.8maven.compiler.source>
    <maven.compiler.target>1.8maven.compiler.target>
  properties>

<dependencies>
        
        <dependency>
            <groupId>junitgroupId>
            <artifactId>junitartifactId>
            <version>4.12version>
            <scope>testscope>
        dependency>
        
        <dependency>
            <groupId>javax.servletgroupId>
            <artifactId>servlet-apiartifactId>
            <version>2.5version>
        dependency>
        <dependency>
            <groupId>javax.servlet.jspgroupId>
            <artifactId>javax.servlet.jsp-apiartifactId>
            <version>2.3.3version>
        dependency>
        <dependency>
            <groupId>jstlgroupId>
            <artifactId>jstlartifactId>
            <version>1.2version>
        dependency>
        
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-contextartifactId>
            <version>5.0.8.RELEASEversion>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-jdbcartifactId>
            <version>5.0.8.RELEASEversion>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-testartifactId>
            <version>5.0.8.RELEASEversion>
        dependency>
        
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-webmvcartifactId>
            <version>5.0.8.RELEASEversion>
        dependency>

        
        <dependency>
            <groupId>org.mybatisgroupId>
            <artifactId>mybatisartifactId>
            <version>3.4.6version>
        dependency>
        <dependency>
            <groupId>org.mybatisgroupId>
            <artifactId>mybatis-springartifactId>
            <version>1.3.2version>
        dependency>
        
        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>druidartifactId>
            <version>1.1.12version>
        dependency>

        
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <version>8.0.13version>
        dependency>
    dependencies>

  <build>
    <finalName>springSpringMVCMyBatis01finalName>
    <pluginManagement>
      <plugins>
        <plugin>
          <artifactId>maven-clean-pluginartifactId>
          <version>3.1.0version>
        plugin>
        
        <plugin>
          <artifactId>maven-resources-pluginartifactId>
          <version>3.0.2version>
        plugin>
        <plugin>
          <artifactId>maven-compiler-pluginartifactId>
          <version>3.8.0version>
        plugin>
        <plugin>
          <artifactId>maven-surefire-pluginartifactId>
          <version>2.22.1version>
        plugin>
        <plugin>
          <artifactId>maven-war-pluginartifactId>
          <version>3.2.2version>
        plugin>
        <plugin>
          <artifactId>maven-install-pluginartifactId>
          <version>2.5.2version>
        plugin>
        <plugin>
          <artifactId>maven-deploy-pluginartifactId>
          <version>2.8.2version>
        plugin>
      plugins>
    pluginManagement>
  build>
project>

3、创建数据库中的表以及实体类;

3.1、MySQL数据库创建表

create table T_XXX{
	id INT PRIMARY KEY AUTO_INCREMENT,
	name varchar(30),
	dat date
}

3.2、对应数据库创建实体类

  • 表中字段与实体字段一一对应
public class xxx{
    1、私有属性字段
	2、构造方法(有参、无参)
	3、getter、setter
	4、hashcode、equals
	5、toString
}

4、 dao层(创建接口)

public interface XXXDao {
    public int insert(XXX XXX);
    public int delete(XXX XXX);
    public int update(XXX XXX);
    public List<XXX> selectAll();
    public XXX selectOne(int id);
}

5、 根据dao创建配置对应的mapper文件(xml)


<mapper namespace="com.某某某.dao.xxxDao">

    <insert id="insert" parametername="com.某某某.model.XXX">
        INSERT INTO t_XXX (id, name, dat)
        VALUES
          (#{id},#{name} , #{dat})
    insert>

    <delete id="delete" parametername="int">
      DELETE
        FROM
          t_XXX
        WHERE id = #{id}
    delete>

    <update id="update" parametername="com.某某某.model.XXX">
        UPDATE
          t_XXX
        SET
          name = #{name},
          dat = #{dat}
        WHERE id = #{id}
    update>

    <select id="selectOne" parametername="int" resultname="com.某某某.model.XXX">
        SELECT
          id,
          name,
          dat
        FROM
          t_XXX
        WHERE id = #{id}
    select>

    <select id="selectAll" resultname="com.某某某.model.XXX">
        SELECT
          id,
          name,
          dat
        FROM
          t_XXX
        LIMIT 0, 1000
    select>

mapper>

6、配置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.xxx.servive"/>
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
        <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/MySQL所用的数据库?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT"/>
        <property name="username" value="MySQL数据库账号"/>
        <property name="password" value="密码"/>

        <property name="initialSize" value="1" />
        <property name="minIdle" value="1" />
        <property name="maxActive" value="20" />
        <property name="testOnReturn" value="false" />
    </bean>
    <!-- 配置Session工厂-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/><!--【属性】链接数据源-->
        <property name="mapperLocations" value="classpath:mapper/xxx.xml"/><!--【属性】挂接映射文件-->
        <property name="typeAliasesPackage" value="com.xxx.model"/><!--【属性】别名:默认的包下的对象-->
    </bean>
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.xxx.dao"/>
    </bean>
</beans>

7、 编写对应的service接口及实现类

  • service
public interface XXXService {
    public boolean insert(XXX xxx);
    public boolean delete(XXX xxx);
    public boolean update(XXX xxx);
    public List<XXX> selectAll();
    public XXX selectOne(XXX xxx);
}
  • 实现类
@Service
public class XXXServiceImp implements XXXService {
    @Resource
    private XXXDao xxxDao;
    @Override
    public boolean insert(XXX xxx) {
        return XXXDao.insert(xxx)==1;
    }

    @Override
    public boolean delete(XXX xxx) {
        return XXXDao.delete(xxx)==1;
    }

    @Override
    public boolean update(XXX xxx) {
        return XXXDao.update(xxx)==1;
    }

    @Override
    public List<XXX> selectAll() {
        return XXXDao.selectAll();
    }

    @Override
    public XXX selectOne(XXX xxx) {
        return XXXDao.selectOne(xxx.getId());
    }
}

8、 控制器Controller

检查是否引包 Spring-context、Spring-jdbc、Spring-test

1、配置web.xml

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">

    
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
    listener>
    <context-param>
        <param-name>contextConfigLocationparam-name>
        <param-value>classpath:applicationContext.xmlparam-value>
    context-param>

    
    <servlet>
        <servlet-name>spring-mvcservlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
        <init-param>
            <param-name>contextConfigLocationparam-name>
            <param-value>classpath:spring-mvc.xmlparam-value>
        init-param>
    servlet>
    <servlet-mapping>
        <servlet-name>spring-mvcservlet-name>
        <url-pattern>*.dourl-pattern>
    servlet-mapping>

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

web-app>


2、配置spring-mvc.xml

<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"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">

    
    <context:component-scan base-package="com.zehao.controller"/>
    
    <mvc:annotation-driven/>
    
    <mvc:default-servlet-handler/>
    
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/view/"/>
        <property name="suffix" value=".jsp"/>
    bean>
beans>


9、 junit测试(及早测试,即时测试,及时测试)

你可能感兴趣的:(SSM学习)