您好,登錄后才能下訂單哦!
這篇文章主要介紹“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; /** * @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(); } }
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> { }
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; }
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 { }
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> { }
jdbc.driver=com.mysql.cj.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/test?serverTimezone=GMT%2B8 jdbc.username=root jdbc.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操作數據庫”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注億速云網站,小編會繼續努力為大家帶來更多實用的文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。