SpringMVC框架搭建idea2021.3.2操作数据库的示例详解
作者:松松松松松柯
这篇文章主要介绍了SpringMVC框架搭建idea2021.3.2操作数据库,本文通过示例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
idea激活码激活永久教程:
https://www.jb51.net/article/195962.htm
https://www.jb51.net/article/200652.htm
https://www.jb51.net/article/201899.htm
1.目录
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框架搭建(idea2021.3.2)- 操作数据库的文章就介绍到这了,更多相关SpringMVC搭建idea2021.3.2内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!