idea配置SpringBoot框架

idea配置SpringBoot框架+JSP页面

1.File–>Project

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

最后点完成

2.项目结构

在这里插入图片描述

静态资源(html,css,js等都放在static中)

SpringBoot默认不支持jsp

调试方法:

1.与resources同目录下创建webapp/WEB-INF:jsp放在这里
2.File-->Project Structure
3.Facets-->添加web,然后修改Web Resource Directories路径

在这里插入图片描述

4.设置好后便可以创建jsp
5.修改pom.xml,添加jsp的引擎配置

 <!--springBoot整合JSP添加依赖  -->
        <!--servlet依赖 -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
        </dependency>

        <!--jstl依赖 -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>

        <!--使jsp页面生效 -->
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
        </dependency>

6.修改数据库驱动的版本,经测试 5.1.8可用
<dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.8</version>
            <scope>runtime</scope>
        </dependency>

如果加入的依赖包有报红的地方,更新一下maven就好了,如下图:

在这里插入图片描述

配置文件 application.yml

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    username: root
    password: mysql
    url: jdbc:mysql://localhost:3333/admin?useSSL=false&characterEncoding=utf-8
  mvc:
    view:
      prefix: /WEB-INF/
      suffix: .jsp
#    static-path-pattern: /static/**
server:
  port: 8888
mybatis:
  mapper-locations: classpath:mapperXml/*.xml

基本框架的pom.xml文件

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.4</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>zj.store</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jdbc</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web-services</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.4</version>
        </dependency>
        <!--数据库连接版本改一改,否则启动时连接数据库报错-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.8</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!--引入外部jar包-->
        <dependency>
            <groupId>charside</groupId>
            <artifactId>charside</artifactId>
            <scope>system</scope>
            <version>1</version>
            <systemPath>${pom.basedir}/lib/chartside.jar</systemPath>
            <!--            <systemPath>${project.basedir}/lib/chartside.jar</systemPath>-->
        </dependency>

        <!--springBoot整合JSP添加依赖  -->
        <!--servlet依赖 -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
        </dependency>

        <!--jstl依赖 -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>

        <!--使jsp页面生效 -->
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

框架结构说明

domain

实体类(变量跟数据库字段对应,getter,setter方法)

mapper

接口

//添加注解 Repository注解接口用
@Repository
public interface TUserMapper {
    int insert(TUser record);
}

service

接口

public interface TUserService {
    int insert(TUser record);
}

serviceImpl

接口的实现类

//@Service服务层组件,用于接口的实现类注解
@Service
public class TUserServiceImpl implements TUserService {
    /**
     * 使用变量注入依赖,提示警告信息,不被推荐
     */
    @Autowired  //一种函数,可以将成员变量、方法和构造函数进行标注,来完成自动装配的工作
    private TUserMapper userMapper;
    @Override
    public int insert(TUser record){
        return userMapper.insert(record);
    }
}

controller

控制类

@Controller
//@RestController //这种注解返回的是文本,无法返回页面
@RequestMapping("/account")
public class Account {

    @Autowired
    private TUserService userService;


    @RequestMapping("/addUser")
    public String addUser(){
        System.out.println("进入addUser接口方法");
        return "jsp/addUser";
    }

    @RequestMapping("/insert")
    public void insert(HttpServletRequest request, HttpServletResponse response){
        String username = request.getParameter("username");
        String pwd = request.getParameter("pwd");
        int age = Integer.parseInt(request.getParameter("age"));
        String email = request.getParameter("email");
        String tel_phone = request.getParameter("telphone");
        TUser user = new TUser();
        user.setUsername(username);
        user.setPassword(pwd);
        user.setAge(age);
        user.setEmail(email);
        user.setTelphone(tel_phone);
        int result = userService.insert(user);
        try {
            ChartSideWeb.SendJson(response,  String.valueOf(result));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

mapperXml

数据库mapper文件,对应domain实体类

<?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="zj.store.demo.mapper.TUserMapper">
    <sql id="Base_Colum_List"></sql>
    <!-- resultMap最终还是要将结果映射到po上,type就是指定映射到哪一个po -->
    <!-- id:设置ResultMap的id -->
    <resultMap type="zj.store.demo.domain.TUser" id="BaseResultMap">
        <!-- 定义主键 ,非常重要。如果是多个字段,则定义多个id -->
        <!-- property:主键在pojo中的属性名 -->
        <!-- column:主键在数据库中的列名 -->
        <id property="id" column="id"/>
        <!-- 定义普通属性 -->
        <result property="username" column="username"/>
        <result property="password" column="password"/>
        <result property="age" column="age"/>
        <result property="email" column="email"/>
        <result property="telphone" column="telphone"/>
        <result property="status" column="status"/>
    </resultMap>
    <insert id="insert" parameterType="zj.store.demo.domain.TUser">
    insert into user (username,password,age,email,telphone) values (#{username},#{password},${age},#{email},#{telphone});
    </insert>
    <!-- <insert id="insertSelective" parameterType="com.gx.po.TUser">
     </insert>-->
    <select id="showUserById" resultMap="BaseResultMap" parameterType="Integer">
    select * from user where id=#{id}
    </select>
    <delete id="deleteByPrimaryKey"  parameterType="Integer" >
        delete from user where id in
        <foreach item="id" index="index" collection="array"
                 open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
    <update id="updateUserById" parameterType="zj.store.demo.domain.TUser">
    update user set password=#{password},age=${age},email=#{email},telphone=#{telphone} where id=#{id}
    </update>
    <update id="updateUserStatus" parameterType="zj.store.demo.domain.TUser">
    update user set status=#{status} where id=#{id}
    </update>
    <select id="findAllUser"  resultType="zj.store.demo.domain.TUser">
        select * from user where 1=1
        <!-- 判断条件参数是否为空 -->
        <if test="status !=null and status != 0">
            and status = ${status}
        </if>
        <if test="username != null and username != ''">
            and username like CONCAT('%',#{username},'%')
        </if>
    </select>
</mapper>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值