zoukankan      html  css  js  c++  java
  • springBoot2.2.0+mybatis-xml文件方式+Oracle11g+jsp页面,实现简单的CRUD

    1.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.2.0.RELEASE</version>
    <relativePath /> <!-- lookup parent from repository -->
    </parent>
    <groupId>sxnx-sms</groupId>
    <artifactId>com.nantian</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <name>自己的项目名!!!!!!!!!!!!!!!</name>
    <description>自己的项目描述!!!!!!!!!!!!!!!!</description>

    <properties>
    <java.version>1.8</java.version>
    </properties>

    <dependencies>
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
    <groupId>com.oracle.ojdbc</groupId>
    <artifactId>ojdbc8</artifactId>
    <scope>runtime</scope>
    </dependency>
    <dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <optional>true</optional>
    </dependency>

    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
    <exclusions>
    <exclusion>
    <groupId>org.junit.vintage</groupId>
    <artifactId>junit-vintage-engine</artifactId>
    </exclusion>
    </exclusions>
    </dependency>
    <!--引入druid数据源 -->
    <dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.1.12</version>
    </dependency>
    <!--spring整合mybatis 暂时 -->
    <dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>1.3.2</version>
    </dependency>
    <!-- 实现访问jsp页面 -->
    <dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
    </dependency>
    <dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-jasper</artifactId>
    <version>7.0.59</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/cn.easyproject/orai18n -->
    <dependency>
    <groupId>cn.easyproject</groupId>
    <artifactId>orai18n</artifactId>
    <version>12.1.0.2.0</version>
    </dependency>

    </dependencies>

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

    </project>

    2.项目目录

     

     注意:web.xml(是自己加进去的,里面没有其他内容,maven-->Dynamic Web Project,不知道当初报啥错添加的web.xml,忘了!!!!)

     3.UserMapper.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="com.nantian.dao.UserDao">
    <!-- CRUD -->
    <!--向数据库插入数据 -->
    <insert id="insert">
    insert into users(name,age,id) values (#{name},#{age},#{id})
    </insert>
    <!--根据id查出一条记录 -->
    <select id="findUserByID" parameterType="com.nantian.pojo.User" resultType="com.nantian.pojo.User">
    select name,age,id from users where id=#{id}
    </select>
    <!-- 查出全部用户 -->
    <select id="findAllUsers" >
    select name,age,id from users
    </select>
    <!--根据id更新用户 -->
    <update id="updateById">
    update users set name=#{name} where id=#{id}
    </update>
    <!-- 根据id删除用户 -->
    <delete id="deleteById" >
    delete from users where id=#{id}
    </delete>
    </mapper>

    4.UserDao.java

    package com.nantian.dao;
    import java.util.Collection;
    import org.apache.ibatis.annotations.Mapper;
    import org.apache.ibatis.annotations.Param;
    import com.nantian.pojo.User;


    @Mapper
    public interface UserDao{
    /**
    * 插入name,age,id
    */
    int insert(@Param("name") String name, @Param("age") Integer age, @Param("id") Integer id);
    /**
    * 根据id查询数据库一条记录
    */
    User findUserByID(@Param("id") Integer id);
    /**
    * 获取所有用户
    */
    Collection<User> findAllUsers();
    /**
    * 根据id,修改用户name和age
    * (没有实现)
    */
    int updateById(@Param("id") Integer id,@Param("name") String name);
    /**
    * 根据id删除用户
    */
    int deleteById(@Param("id") Integer id);
    }

    5.UserService.java

    package com.nantian.service;

    import java.util.Collection;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    import com.nantian.dao.UserDao;
    import com.nantian.pojo.User;


    @Service
    public class UserService {
    @Autowired
    private UserDao userDao;
    /**
    * 增加
    */
    public int insertUser(String name,Integer age,Integer id) {
    userDao.insert(name, age, id);
    return age;
    }
    /**
    * 根据id查找一条记录
    */
    public User findUserById(Integer id){
    return userDao.findUserByID(id);
    }
    /**
    * 获取所有用户
    */
    public Collection<User> findAllUsers(){
    return userDao.findAllUsers();
    }
    /**
    * 根据id,修改用户name和age
    */
    public int updateById(Integer id,String name) {
    return userDao.updateById(id,name);
    }
    /**
    * 根据id删除用户
    */
    public int deleteById(Integer id) {
    return userDao.deleteById(id);
    }
    }

    6.1 UserController

    package com.nantian.controller;

    import java.util.Collection;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    import com.nantian.pojo.User;
    import com.nantian.service.UserService;

    @RestController
    public class UserController {
    @Autowired
    private UserService userService;
    /**
    * 向数据库插入值
    */
    @RequestMapping("/insertUser")
    public int insertUser(String name,Integer age,Integer id) {
    return userService.insertUser(name, age, id);
    }
    /**
    * 根据id查询单条记录
    */
    @RequestMapping("/findUserById")
    public User findUserById(Integer id) {
    return userService.findUserById(id);
    }
    /**
    * 获取所有用户
    */
    @RequestMapping("/findAllUsers")
    public Collection<User> findAllUsers(){
    return userService.findAllUsers();
    }
    /**
    * 根据id,修改用户name和age
    */
    //修改
    @RequestMapping("/updateById")
    public int updateById(Integer id,String name) {
    return userService.updateById(id,name);
    }
    /**
    * 根据id删除用户
    */
    @RequestMapping("/deleteById")
    public int deleteById(Integer id) {
    return userService.deleteById(id);
    }
    }

    6.2  UserJspController.java专门是访问jsp页面的前台类

    package com.nantian.controller;

    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;

    @Controller
    public class UserJspController {
    @RequestMapping("/user")
    public String index() {
    return "user";
    }
    }

    7.启动类没有做任何修改

    package com.nantian;

    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;

    @SpringBootApplication
    public class Application {

    public static void main(String[] args) {
    SpringApplication.run(Application.class, args);
    }

    }

    8.index.jsp

    <%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>SpringMVC</title>
    </head>
    <body>
    <h1>恭喜!!!成功操作!!!</h1>
    </body>
    </html>

    9.user.jsp

    <%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>测试CRUD</title>
    </head>
    <body>
    <form action="/user" method="GET">
    <table>
    <tr>
    <td>用户名:</td>
    <td>
    <input type="text" name="name"/>
    </td>
    </tr>
    <tr>
    <td>ID:</td>
    <td>
    <input type="text" name="id"/>
    </td>
    </tr>
    <tr>
    <td>年龄:</td>
    <td>
    <input type="text" name="age"/>
    </td>
    </tr>
    </table>
    <button type="submit" value="Submit">增加</button>
    </form>
    </body>
    </html>

    10.application.yml配置文件

    server:
    port: 8080
    servlet:
    context-path: /
    spring:
    mvc:
    view:
    prefix: /WEB-INF/jsp/
    suffix: .jsp
    datasource:
    #引入druid数据源
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: oracle.jdbc.driver.OracleDriver
    url: jdbc:oracle:thin:@localhost:1521:orcl
    username: scott
    password: Xjj141093

    #整合mybatis
    mybatis:
    #别名包
    type-aliases-package: com.nantian.pojo
    #引入映射文件
    mapper-locations: classpath:/mybatis/mappers/*.xml
    #开启驼峰映射
    configuration:
    map-underscore-to-camel-case: true

    11.项目启动方式.项目右键-->run as-->spring boot app方式启动

    还有啥问题,欢迎大家留言指正!!!

  • 相关阅读:
    BZOJ 3677 连珠线
    BZOJ 3676 回文串
    BZOJ 3675 序列分割
    BZOJ 4013 实验比较
    BZOJ 4011 落忆枫音
    使用Google BBR加速 VPS
    OSX编译安装Python3及虚拟开发环境Virtualenv
    OSX安装Mysql8.0
    OpenSSL编程之摘要
    OpenCA搭建
  • 原文地址:https://www.cnblogs.com/curedfisher/p/11802992.html
Copyright © 2011-2022 走看看