[笔记] SpringMVC+Mybatis搭建Web开发环境

用习惯jFinal之后spring忘完了。。。

再此记个笔记

项目结构如下

首先是依赖关系 我用IDEA的maven处理了 这里数据库框架用的mybatis,数据库是mariadb,向前台传数据用json就用了FASTJSON

<?xml version="1.0" encoding="UTF-8"?>
<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>demo</groupId>
    <artifactId>demo6</artifactId>
    <version>1.0-SNAPSHOT</version>

    <profiles>
        <profile>
            <id>jdk-1.8</id>
            <activation>
                <jdk>1.8</jdk>
            </activation>
            <properties>
                <maven.compiler.source>1.8</maven.compiler.source>
                <maven.compiler.target>1.8</maven.compiler.target>
                <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
            </properties>
        </profile>
    </profiles>

    <build>
        <defaultGoal>compile</defaultGoal>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                    <include>**/*.properties</include>
                </includes>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/**</include>
                </includes>
            </resource>
        </resources>
    </build>

    <dependencies>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.38</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.1.2.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>5.1.2.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.1.2.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>2.0.2</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.5</version>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.1.36</version>
        </dependency>
    </dependencies>
</project>

然后是spring的配置文件 applicationContext.xml 名字随便改 只要和web.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"
       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 http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <context:component-scan base-package="cn.erika.*"/>
    <mvc:annotation-driven/>

    <bean id="dataConfig"
          class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location" value="classpath:datasource.properties"/>
    </bean>

    <bean id="dataSource"
          class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${driver}">
        </property>
        <property name="url" value="${url}">
        </property>
        <property name="username" value="${username}">
        </property>
        <property name="password" value="${password}">
        </property>
    </bean>

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <!-- 我的mybatis只需要配置一下mapper文件的位置 如果需要别的配置建议单独写mybatis的配置文件在这里引用 -->
        <property name="mapperLocations" value="classpath:cn/erika/mapper/*.xml"/>
    </bean>

    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate" destroy-method="close">
        <constructor-arg ref="sqlSessionFactory"/>
    </bean>
</beans>

显然数据源的配置文件你们自己写就好 我用的properties的 也就是k-v格式的配置 此处省略了

接下来配置web.xml 添加如下内容即可 这里和一般的selvet一样

<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:applicationContext.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>

到这里环境就已经打完了 之后是测试数据

User.java的代码就不放了,无聊的代码

UserMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.erika.mapper.UserMapper">
    <resultMap id="UserEntry" type="cn.erika.model.User">
        <id property="id" column="id"/>
        <result property="name" column="name"/>
        <result property="password" column="password"/>
        <result property="type" column="type"/>
        <result property="lastLogin" column="last_login"/>
        <result property="desc" column="desc"/>
    </resultMap>

    <select id="getAll" resultMap="UserEntry">
        SELECT * FROM tb_user
    </select>
</mapper>

UserEntry.java也没啥东西 仅为了减少代码量

UserService.java

package cn.erika.service;

import cn.erika.mapper.UserMapper;
import cn.erika.model.User;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("userService")
public class UserService {
    private UserMapper mapper;

    @Autowired
    public UserService(@Qualifier("sqlSession") SqlSession session) {
        this.mapper = session.getMapper(UserMapper.class);
    }

    public List<User> getAll() {
        return mapper.getAll();
    }
}

DemoController.java

package cn.erika.controller;

import cn.erika.service.UserService;
import com.alibaba.fastjson.JSON;
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;

@Controller
@RequestMapping("/demo")
public class DemoController{

    @Autowired
    private UserService service;

    @RequestMapping("getAll")
    @ResponseBody
    public String getAll(){
        String json = JSON.toJSONString(service.getAll());
        return json;
    }
}

后来在运行的时候怎么都跑不起来,然后发现是部署的问题

把lib都扔进去 然后就好了。。。 郁闷 害得我百度半天没找到原因

posted on 2019-07-24 12:33  绝对密位  阅读(244)  评论(0编辑  收藏  举报

导航