• 搭建SSM项目,从数据库到前台数据显示 2019


    最近接受到要求,要我们使用sql server数据库  

    为了方便测试代码,我决定自己搭建一个SSM框架,正好好久没搭建框架了,什么东西都忘光了,顺便复习一下

    当然很多都是百度现成的 加上自己稍微修改 就搞定了

    首先肯定是创建一个maven项目啦

    当然大前提是搭建eclipse环境..如果没搭建..就百度搭建好了再来..

    先弄个pom

    <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.0</modelVersion>
    <groupId>test</groupId>
    <artifactId>test</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>


    <dependencies>
    <!-- 单元测试 -->
    <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>3.8.1</version>
    <scope>test</scope>
    </dependency>
    <!-- spring核心包 -->
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-test</artifactId>
    <version>4.3.13.RELEASE</version>
    </dependency>
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>4.3.13.RELEASE</version>
    </dependency>
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>4.3.13.RELEASE</version>
    </dependency>
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-aop</artifactId>
    <version>4.3.13.RELEASE</version>
    </dependency>
    <dependency>
    <groupId>aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
    <version>1.5.4</version>
    </dependency>
    <dependency>
    <groupId>aspectj</groupId>
    <artifactId>aspectjrt</artifactId>
    <version>1.5.4</version>
    </dependency>
    <dependency>
    <groupId>org.codehaus.jackson</groupId>
    <artifactId>jackson-mapper-asl</artifactId>
    <version>1.9.11</version>
    </dependency>
    <!--mybatis核心包 -->
    <dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis</artifactId>
    <version>3.1.1</version>
    </dependency>
    <dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis-spring</artifactId>
    <version>1.1.1</version>
    </dependency>
    <!-- <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId>
    <version>5.1.21</version> </dependency> -->
    <!-- 原先是mysql 改為 server -->
    <dependency>
    <groupId>com.microsoft.sqlserver</groupId>
    <artifactId>sqljdbc4</artifactId>
    <version>4.0</version>

    </dependency>

    <dependency>
    <groupId>c3p0</groupId>
    <artifactId>c3p0</artifactId>
    <version>0.9.1.2</version>
    <type>jar</type>
    </dependency>
    <!--打印日志 -->
    <dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.7.5</version>
    </dependency>
    <dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-log4j12</artifactId>
    <version>1.7.5</version>
    </dependency>
    <dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.17</version>
    </dependency>
    <dependency>
    <groupId>jstl</groupId>
    <artifactId>jstl</artifactId>
    <version>1.2</version>
    </dependency>
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context-support</artifactId>
    <version>4.3.13.RELEASE</version>

    </dependency>
    <!-- mybatis分页插件 -->
    <!-- <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId>
    <version>4.1.4</version> </dependency> --> <!-- javax.servlet.jsp-api -->
    <dependency>
    <groupId>javax.servlet.jsp</groupId>
    <artifactId>javax.servlet.jsp-api</artifactId>
    <version>2.2.1</version>
    <scope>provided</scope>
    </dependency>
    <!-- javax.servlet-api -->
    <dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.1.0</version>
    <scope>provided</scope>
    </dependency>

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

    <!-- spring-webmvc -->
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>4.3.13.RELEASE</version>
    </dependency>
    <!-- 阿里巴巴fastjson -->
    <dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.41</version>
    </dependency>
    <!--dbcp -->
    <dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-dbcp2</artifactId>
    <version>2.1.1</version>
    </dependency>

    <!-- 辅助 -->
    <dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    <version>2.2.4</version>
    </dependency>

    <dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.3.2</version>
    </dependency>

    <!-- end -->

    </dependencies>
    <build>

    <plugins>
    <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>2.0.2</version>
    <configuration>
    <source>1.8</source>
    <target>1.8</target>
    </configuration>
    </plugin>

    </plugins>
    </build>
    </project>

    然后就是resource目录下放配置文件啊 ~

    第三个文件是转pdf验证文件 搭建是用不到的

    application-config.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:p="http://www.springframework.org/schema/p"
    xmlns:c="http://www.springframework.org/schema/c" xmlns:util="http://www.springframework.org/schema/util"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:ehcache="http://www.springmodules.org/schema/ehcache"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
    http://www.springmodules.org/schema/ehcache http://www.springmodules.org/schema/cache/springmodules-ehcache.xsd">

    <!-- 自动扫描(自动注入) -->
    <context:component-scan base-package="test.cn.service" />
    <!-- 配置连接池 -->
    <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">  //数据库连接驱动 数据库
    <property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"></property>
    <property name="url" value="jdbc:sqlserver://localhost:1433;DatabaseName=sc"></property>
    <property name="username" value="sa"></property>
    <property name="password" value="123456"></property>
    </bean>


    <!-- myBatis文件 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <!-- 自动扫描entity目录, 省掉Configuration.xml里的手工配置 -->
    <property name="configLocation" value="classpath:mybatis-config.xml" />
    </bean>

    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="basePackage" value="test.cn.mapper" />                                     //扫描mapper映射目录
    <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
    </bean>
    <!-- 配置事务管理器 -->
    <bean id="transactionManager"
    class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource" />
    </bean>

    <!-- 配置事物的注解方式注入 -->
    <tx:annotation-driven transaction-manager="transactionManager" />

    </beans>

     application-mvc.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:p="http://www.springframework.org/schema/p"
    xmlns:c="http://www.springframework.org/schema/c" xmlns:util="http://www.springframework.org/schema/util"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:ehcache="http://www.springmodules.org/schema/ehcache"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
    http://www.springmodules.org/schema/ehcache http://www.springmodules.org/schema/cache/springmodules-ehcache.xsd">

    <!-- 扫描Controller层 -->
    <context:component-scan base-package="test.cn.controller" />

    <!-- 开启注解 -->
    <mvc:annotation-driven />


    <!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 -->
    <bean
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/view/" />     //指向路径 ~
    <property name="suffix" value=".jsp" />     <!--可为空,方便实现自已的依据扩展名来选择视图解释类的逻辑 -->
    </bean>

     // 加载静态文件

    <mvc:resources location="/file/" mapping="/file/**" />


    <!-- 加了这个才可以上传  如果想要上传还要添加pom文件相应的地址--> 
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" />


    </beans>

    log4j.properties

    打印日志用的 为了更好的找错

    mybatis-config.xml      这个文件只是一个摆设 但是节省不了 

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
    <configuration>
    <typeAliases>
    <!-- 给cn.entity包下起别名 -->
    <package name="test.cn.entity" />

    </typeAliases>

    </configuration>

    web.xml   这个文件是最容易出错的地方

    <?xml version="1.0" encoding="UTF-8"?>
    <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" version="2.5">
    <display-name>b2b</display-name>
    <!-- Spring-mybatis的配置文件 -->
    <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:application-config.xml</param-value>
    </context-param>

    <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>


    <!-- spring mvc servlet -->
    <servlet>
    <description>spring mvc servlet</description>
    <servlet-name>springMvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:application-mvc.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
    <servlet-name>springMvc</servlet-name>
    <url-pattern>/</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.jpg</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.js</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.css</url-pattern>
    </servlet-mapping>

    <filter>
    <filter-name>encodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>

    <param-name>encoding</param-name>
    <param-value>UTF-8</param-value>
    </init-param>
    </filter>
    <filter-mapping>
    <filter-name>encodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
    </filter-mapping>

    <!-- DEFAULT PAGE -->
    <welcome-file-list>
    <welcome-file>/WEB-INF/view/index.jsp</welcome-file>
    </welcome-file-list>




    </web-app>

    然后就起始页 http://localhost:8080/test  直接

    直接进首页啦   一般来说 是没问题的 如果有 应该是你配置的问题 这就千奇百怪啦 那自己慢慢百度 一一举例太累了也~

    接下来就是 数据库数据到前台的显示  这个很简单

    先创建文件

    这是实体类

    package test.cn.entity;
    /**
    *
    * @author 首页banner图
    *
    */
    public class Banner {

    private Integer id;
    private String imgurl;
    private String navigator;
    private Integer soft;

    public Integer getId() {
    return id;
    }
    public void setId(Integer id) {
    this.id = id;
    }

    public String getImgurl() {
    return imgurl;
    }
    public void setImgurl(String imgurl) {
    this.imgurl = imgurl;
    }
    public String getNavigator() {
    return navigator;
    }
    public void setNavigator(String navigator) {
    this.navigator = navigator;
    }
    public Integer getSoft() {
    return soft;
    }
    public void setSoft(Integer soft) {
    this.soft = soft;
    }



    }

    接下啦是mapper

    接口+配置文件

    BannerMapper.java



    public interface BannerMapper {

    public List<Banner> getbannerall();


    }

    BannerMapper.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    <mapper namespace="test.cn.mapper.BannerMapper">

    <!-- 前六条数据 排序 -->
    <select id="getbannerall" resultType="Banner">

    SELECT TOP 6 imgurl,navigator
    FROM banner ORDER BY sort


    </select>

    </mapper>

    server层

    BannerService.java



    public interface BannerService {

    public List<Banner> getbannerall();

    }

    实现   BannerServiceImpl.java

    @Service
    public class BannerServiceImpl implements BannerService {

    @Autowired
    BannerMapper bannermapper;

    @Override
    public List<Banner> getbannerall() {

    return bannermapper.getbannerall();
    }

    }

    然后controller层访问

    import java.util.List;

    @Controller
    @RequestMapping("/banner")
    public class BannerController {

    @Autowired
    private BannerService bannerservice;



    @RequestMapping(name = "/getbannerlist",method = RequestMethod.GET)
    @ResponseBody
    public List<Banner> getbannerlist(){


    return bannerservice.getbannerall();
    }



    @RequestMapping("/gohome")
    public ModelAndView gotohome() {

    ModelAndView mv=new ModelAndView("home");
    mv.addObject("banner",bannerservice.getbannerall());
    return mv;
    }



    }

    好了 这是访问地址  http://localhost:test/b2b/banner/gohome

    对象就打印在前台啦 这就代表前后台贯通啦 ~

  • 相关阅读:
    LINQ体验(6)——LINQ语句之Join和Order By
    转 Spring.NET 与 NHibernate 的整合
    项目经理人必须要遵循的14个成功原则
    做成功的项目经理人
    就业模拟试题_Net(答案)
    java中的23中设计模式
    七种武器——.NET工程师求职面试必杀技
    如何识别真正的程序员
    WCF传输大数据的设置
    就业模拟试题_Java(答案)
  • 原文地址:https://www.cnblogs.com/Mr-Y1907/p/11282463.html
Copyright © 2020-2023  润新知