温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

SpringMVC框架怎么搭建idea操作数据库

发布时间:2022-04-01 13:44:39 来源:亿速云 阅读:244 作者:iii 栏目:开发技术

这篇文章主要介绍“SpringMVC框架怎么搭建idea操作数据库”,在日常操作中,相信很多人在SpringMVC框架怎么搭建idea操作数据库问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”SpringMVC框架怎么搭建idea操作数据库”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

1.目录

SpringMVC框架怎么搭建idea操作数据库

2.PersonController

package com.sk.controller;
import com.sk.entity.Person;
import com.sk.service.PersonService;
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;
/**
 * @Author 松柯
 * @Date 2022/3/31 17:36
 * @Version 1.0
 */
@Controller
@RequestMapping("/com/sk/Person")
public class PersonController {
    @Autowired
   private PersonService personService;
    @RequestMapping("/getPersonById")
    @ResponseBody
    public Object getPersonById(Integer id){
        return personService.getById(id);
    }
    @RequestMapping("/savePerson")
    @ResponseBody
    public Boolean savePerson(Person person){
        return personService.save(person);
    }
    @RequestMapping("/getPersonList")
    @ResponseBody
    public Object getPersonList(){
        return personService.list();
    }
}

3.PersonMapper

package com.sk.dao;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.sk.entity.Person;
/**
 * @Author 松柯
 * @Date 2022/3/31 17:36
 * @Version 1.0
 */
public interface PersonMapper extends BaseMapper<Person> {
}

4.Person

package com.sk.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import lombok.Data;
/**
 * @Author 松柯
 * @Date 2022/3/31 17:30
 * @Version 1.0
 */
@Data
public class Person {
    /**
     * personID
     */
    @TableId(type = IdType.ASSIGN_ID)
    private String personId;
     * 人名
    private String personName;
     * 年龄
    private Integer personAge;
}

5.PersonServiceImpl

package com.sk.service.Impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sk.dao.PersonMapper;
import com.sk.entity.Person;
import com.sk.service.PersonService;
import org.springframework.stereotype.Service;
/**
 * @Author 松柯
 * @Date 2022/3/31 17:35
 * @Version 1.0
 */
@Service
public class PersonServiceImpl extends ServiceImpl<PersonMapper, Person> implements PersonService {
}

6.PersonService

package com.sk.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.sk.entity.Person;
import org.apache.ibatis.annotations.Mapper;
/**
 * @Author 松柯
 * @Date 2022/3/31 17:35
 * @Version 1.0
 */
public interface PersonService extends IService<Person> {
}

7.jdbc.properties

jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test?serverTimezone=GMT%2B8
jdbc.username=root
jdbc.password=123456

8.springmvc-servlet.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:mvc="http://www.springframework.org/schema/mvc"
       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/mvc
        https://www.springframework.org/schema/mvc/spring-mvc.xsd
         http://www.springframework.org/schema/context
         https://www.springframework.org/schema/context/spring-context.xsd">

    <!--扫描包下的注解-->
    <context:component-scan base-package="com.sk"/>
    <!-- 导入资源文件 -->
    <context:property-placeholder location="classpath:jdbc.properties"/>
    <!-- DAO接口所在包名,Spring会自动查找其下的类 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.sk.dao" />
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
    </bean>
    <!--mybatisPlus的SqlSessionFactoryBean-->
    <bean class="com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean" id="sqlSessionFactory">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${jdbc.driver}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>
    <!--在springmvc-servlet.xml中配置<mvc:default-servlet-handler />后,
    会在Spring MVC上下文中定义一个org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler,
    它会像一个检查员,对进入DispatcherServlet的URL进行筛查,
    如果发现是静态资源的请求,
    就将该请求转由Web应用服务器默认的Servlet处理,
    如果不是静态资源的请求,才由DispatcherServlet继续处理。-->
    <!--静态页面,如html,css,js,images可以访问-->
    <mvc:default-servlet-handler/>
    <!--Spring 3.0.x中使用了mvc:annotation-driven后,
    默认会帮我们注册默认处理请求,参数和返回值的类,
    其中最主要的两个类:DefaultAnnotationHandlerMapping 和 AnnotationMethodHandlerAdapter ,
    分别为HandlerMapping的实现类和HandlerAdapter的实现类,
    从3.1.x版本开始对应实现类改为了RequestMappingHandlerMapping和RequestMappingHandlerAdapter。-->
    <!--注解驱动,以使得访问路径与方法的匹配可以通过注解配置-->
    <mvc:annotation-driven/>

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>

9.sql

-------------person----------------
CREATE TABLE `test` (
  `person_id` int NOT NULL AUTO_INCREMENT COMMENT '主键id',
  `person_name` varchar(255) NULL COMMENT '人名',
  `person_age` int NULL COMMENT '年龄',
  PRIMARY KEY (`person_id`)
);
-------------------------------------

10.pom

<?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>org.example</groupId>
    <artifactId>spring-mvc</artifactId>
    <version>1.0-SNAPSHOT</version>
    <properties>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
    </properties>
    <dependencies>
        <!--spring 核心包-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>5.3.16</version>
        </dependency>
            <artifactId>spring-web</artifactId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.3.17</version>
            <artifactId>spring-aop</artifactId>
        <!--引入jquery依赖-->
        <!-- https://mvnrepository.com/artifact/org.webjars.bower/jquery -->
            <groupId>org.webjars.bower</groupId>
            <artifactId>jquery</artifactId>
            <version>3.6.0</version>
        <!-- lombok插件 -->
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.20</version>
        <!-- https://mvnrepository.com/artifact/com.baomidou/mybatis-plus -->
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus</artifactId>
            <version>3.3.1</version>
        <!-- Mysql数据库链接包 -->
        <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.28</version>
        <!-- Druid数据库连接池包 -->
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.9</version>
        <!-- sqlServer数据库 -->
            <groupId>com.microsoft.sqlserver</groupId>
            <artifactId>mssql-jdbc</artifactId>
            <version>8.4.1.jre8</version>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
            <artifactId>spring-context</artifactId>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-expression -->
            <artifactId>spring-expression</artifactId>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-tx -->
            <artifactId>spring-tx</artifactId>
            <version>4.3.22.RELEASE</version>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>2.9.3</version>
        <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
            <artifactId>jackson-core</artifactId>
        <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
            <artifactId>jackson-databind</artifactId>
            <artifactId>spring-jdbc</artifactId>
    </dependencies>
</project>

到此,关于“SpringMVC框架怎么搭建idea操作数据库”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI