SpringMVC框架搭建idea2021.3.2操作數(shù)據(jù)庫(kù)的示例詳解
idea激活碼激活永久教程:
http://www.dbjr.com.cn/article/195962.htm
http://www.dbjr.com.cn/article/200652.htm
http://www.dbjr.com.cn/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"/> <!-- 導(dǎo)入資源文件 --> <context:property-placeholder location="classpath:jdbc.properties"/> <!-- DAO接口所在包名,Spring會(huì)自動(dòng)查找其下的類 --> <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 />后, 會(huì)在Spring MVC上下文中定義一個(gè)org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler, 它會(huì)像一個(gè)檢查員,對(duì)進(jìn)入DispatcherServlet的URL進(jìn)行篩查, 如果發(fā)現(xiàn)是靜態(tài)資源的請(qǐng)求, 就將該請(qǐng)求轉(zhuǎn)由Web應(yīng)用服務(wù)器默認(rèn)的Servlet處理, 如果不是靜態(tài)資源的請(qǐng)求,才由DispatcherServlet繼續(xù)處理。--> <!--靜態(tài)頁(yè)面,如html,css,js,images可以訪問(wèn)--> <mvc:default-servlet-handler/> <!--Spring 3.0.x中使用了mvc:annotation-driven后, 默認(rèn)會(huì)幫我們注冊(cè)默認(rèn)處理請(qǐng)求,參數(shù)和返回值的類, 其中最主要的兩個(gè)類:DefaultAnnotationHandlerMapping 和 AnnotationMethodHandlerAdapter , 分別為HandlerMapping的實(shí)現(xiàn)類和HandlerAdapter的實(shí)現(xiàn)類, 從3.1.x版本開(kāi)始對(duì)應(yīng)實(shí)現(xiàn)類改為了RequestMappingHandlerMapping和RequestMappingHandlerAdapter。--> <!--注解驅(qū)動(dòng),以使得訪問(wèn)路徑與方法的匹配可以通過(guò)注解配置--> <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數(shù)據(jù)庫(kù)鏈接包 --> <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.28</version> <!-- Druid數(shù)據(jù)庫(kù)連接池包 --> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.9</version> <!-- sqlServer數(shù)據(jù)庫(kù) --> <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>
到此這篇關(guān)于SpringMVC框架搭建(idea2021.3.2)- 操作數(shù)據(jù)庫(kù)的文章就介紹到這了,更多相關(guān)SpringMVC搭建idea2021.3.2內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java實(shí)現(xiàn)文件上傳到服務(wù)器本地并通過(guò)url訪問(wèn)的方法步驟
最近項(xiàng)目中使用到了文件上傳到服務(wù)器的功能,下面這篇文章主要給大家介紹了關(guān)于Java實(shí)現(xiàn)文件上傳到服務(wù)器本地并通過(guò)url訪問(wèn)的方法步驟,文中通過(guò)圖文以及實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-04-04Java服務(wù)中的大文件上傳和下載優(yōu)化技巧分享
在Java服務(wù)中處理大文件的上傳和下載是一項(xiàng)常見(jiàn)但復(fù)雜的任務(wù),為了提供優(yōu)秀的用戶體驗(yàn)和高效的系統(tǒng)性能,我們將探索多種策略和技術(shù),并在每一點(diǎn)上都提供代碼示例以便實(shí)戰(zhàn)應(yīng)用,需要的朋友可以參考下2023-10-10