共 1 篇文章

标签:简明易懂:Spring MVC数据库连接配置 (spring mvc配置数据库连接)

简明易懂:Spring MVC数据库连接配置 (spring mvc配置数据库连接)

Spring MVC是一款优秀的Web开发框架,它以MVC设计模式为核心,通过注解和配置文件将Controller、Service、DAO、数据源、事务等功能进行整合,为我们在Web开发中带来了很大的方便。 在Spring MVC中进行数据库操作,需要先进行连接数据库的配置。今天,我们就来介绍一下Spring MVC数据库连接配置的方法。 一、添加依赖 在pom.xml配置文件中添加数据源依赖: “` com.alibaba druid-spring-boot-starter 1.2.6 “` 二、配置数据源 在application.yml(或application.properties)配置文件中添加以下内容: “` spring.datasource.url=jdbc:mysql://localhost:3306/test?useSSL=false&characterEncoding=UTF-8&serverTimezone=UTC spring.datasource.username=root spring.datasource.password=123456 spring.datasource.driver-class-name=com.mysql.jdbc.Driver “` 其中,url是连接数据库的URL地址,username和password是连接数据库的用户名和密码,driver-class-name是数据库驱动名称。 三、配置MyBatis 如果要使用MyBatis进行数据库操作,需要进行如下配置: 1.添加依赖: “` org.mybatis.spring.boot mybatis-spring-boot-starter 2.1.3 “` 2.在application.yml(或application.properties)配置文件中添加以下内容: “` mybatis: type-aliases-package: com.xxx.xxx.domn mapper-locations: classpath:mapper/*.xml “` 其中,type-aliases-package是实体类所在的包名,mapper-locations是Mapper配置文件所在的路径。 四、配置事务管理器 如果需要使用事务管理器,需要进行如下配置: 1.添加依赖: “` org.springframework.boot spring-boot-starter-jdbc “` 2.在application.yml(或application.properties)配置文件中添加以下内容: “` spring.jmx.enabled=true spring.datasource.continue-on-error=true spring.datasource.schema=classpath:db/schema.sql spring.datasource.sql-script-encoding=UTF-8 spring.datasource.initialize=true spring.datasource.platform=mysql spring.jpa.hibernate.ddl-auto=none spring.jpa.show-sql=false spring.datasource.type: com.alibaba.druid.pool.DruidDataSource spring.datasource.url=jdbc:mysql://localhost:3306/test?useSSL=false&characterEncoding=UTF-8&serverTimezone=UTC spring.datasource.username=root spring.datasource.password=123456 spring.datasource.driver-class-name=com.mysql.jdbc.Driver mybatis: type-aliases-package: com.xxx.xxx.domn mapper-locations: classpath:mapper/*.xml spring.datasource.max-active=20 spring.datasource.initial-size=1 spring.datasource.min-idle=1 spring.datasource.validation-query=select 1 spring.datasource.test-on-borrow=true spring.datasource.pool-prepared-statements=true spring.datasource.max-pool-prepared-statement-per-connection-size=20 spring.datasource.filters=stat,wall,log4j spring.datasource.connection-properties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000 “` 其中,需要注意配置的是事务管理器的类型为com.atomikos.jdbc.AtomikosDataSourceBean,这是Spring Boot默认的事务管理器,需要在类路径下添加如下配置文件: “` # jta.properties com.atomikos.icatch.service=com.atomikos.icatch.standalone.UserTransactionServiceFactory com.atomikos.icatch.log_base_dir=./tmlog com.atomikos.icatch.output_dir=./ com.atomikos.icatch.console_file=./tm_console.log com.atomikos.icatch.console_listeners=com.atomikos.icatch.standalone.ConsoleLogManager “` 到此,Spring MVC数据库连接配置就介绍完了。希望对大家有所帮助。 相关问题拓展阅读: spring mvc mybatis 整合 大体步骤 spring mvc mybatis 整合 大体步骤 一、简单说明 用到的框架:spring、springmvc,mybatis 开发工具:eclipse,apache-tomcat-6.0.39 jar包管理:maven 开发过程 一、建立工程 1、引入相关jar包: junit junit 4.8.1 test provided, 因为provided表明该包只在编译和测试的时候用–> javax.servlet servlet-api 2.5 provided 二、引入mybatis相关内容并测试 1、引入JAR包...

技术分享