这篇文章主要介绍“springMVC框架怎么搭建idea操作数据库”,在日常操作中,相信很多人在springmvc框架怎么搭建idea操作数据库问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”SpringMVC
这篇文章主要介绍“springMVC框架怎么搭建idea操作数据库”,在日常操作中,相信很多人在springmvc框架怎么搭建idea操作数据库问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”SpringMVC框架怎么搭建idea操作数据库”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
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;@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(); }}
package com.sk.dao;import com.baomidou.mybatisplus.core.mapper.BaseMapper;import com.sk.entity.Person;public interface PersonMapper extends BaseMapper<Person> {}
package com.sk.entity;import com.baomidou.mybatisplus.annotation.IdType;import com.baomidou.mybatisplus.annotation.TableId;import lombok.Data;@Datapublic class Person { @TableId(type = IdType.ASSIGN_ID) private String personId; * 人名 private String personName; * 年龄 private Integer personAge;}
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;@Servicepublic class PersonServiceImpl extends ServiceImpl<PersonMapper, Person> implements PersonService {}
package com.sk.service;import com.baomidou.mybatisplus.extension.service.IService;import com.sk.entity.Person;import org.apache.ibatis.annotations.Mapper;public interface PersonService extends IService<Person> {}
jdbc.driver=com.mysql.cj.jdbc.Driverjdbc.url=jdbc:Mysql://localhost:3306/test?serverTimezone=GMT%2B8jdbc.username=rootjdbc.passWord=123456
<?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>
-------------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`));-------------------------------------
<?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操作数据库”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注编程网网站,小编会继续努力为大家带来更多实用的文章!
--结束END--
本文标题: SpringMVC框架怎么搭建idea操作数据库
本文链接: https://lsjlt.com/news/326158.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0