㈠ 新手spring整合mybatis报错连接数据库失败
问题:运行 mybatis-generator 是报错: Failed to execute goal org.mybatis.generator:mybatis-generator-maven-plugin:1.3.5Access denied for user ‘root‘@‘localhost‘ (using password: NO)
前提是:我在DOS命令下等正常连接mysql,那就排除mysql服务是否启动相关的问题;我是新手,常常犯的错误就是单词拼写错误,那接下来我就比对两个有做mysql配置相关的文件做了检查,一个是application.properties,另一个是 mybatis-generator.xml ,在对比的过程中我就袜判猛发现这冲吵两个文件的mysql密码是对不上的,瞬间six屎啦!告桥
㈡ idea springboot中整合jdbc连接MySQL中,Schemas中无mybatis怎么解决
从你的这张截图上看,你建立了一个空白的springboot项目,并且使用了idea的data base数据手耐库客户端去管理数据库(类似Navicat)。你的这些毕搏春操作,和你问题上描述的,springboot整合jdbc没有直接关联,更别说mybatis了。
springboot整合jdbc或mybatis,需要在你的pom文件中引入相关依赖,然后在application.yml中加入数据库配置银枯。
㈢ springboot连接数据库总是出现断开连接的问题
日志描述:
### Error querying database. Cause: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from the server was 67,631,373 milliseconds ago. The last packet sent successfully to the server was 67,631,375 milliseconds ago. is longer than the server configured value of 'wait_timeout'. You should consider either expiring and/or testing connection validity before use in your application, increasing the server configured values for client timeouts, or using the Connector/J connection property 'autoReconnect=true' to avoid this problem.
如果配置是第一种的话 改为第二种即可
㈣ java中spring配置中连接数据库的代码怎么写
<!-- 配置数据源 -->
<bean id="dataSource" class="org.apache.tomcat.dbcp.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://192.168.2.100:3306/test"></property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
</bean>
<!-- 配置SessionFactory并注入其依赖的数据源 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" /><!-- 注入一个数据源 -->
</property>
<!-- 配置Hibernate的属性 -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<!-- 配置hibernate自动加载持久化映射文件 -->
<property name="mappingResources">
<list>
<value>domain/Person.hbm.xml</value></list>
</property>
</bean>
㈤ spring boot 通过jpa连接mysql失败
步棚喊骤一:在pom.xml文件中添加MYSQl和JPA的相关嫌灶Jar包依赖,具体添加位置在dependencies中,具体添加的内容如下所示链者野。
<!--数据库相关配置-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.11</version>
</dependency>
步骤二:在application.properties配置文件中加入数据库的相关配置,配置信息如下所示。
spring.datasource.url = jdbc:mysql://localhost:3306/webtest
spring.datasource.username = root
spring.datasource.password = 220316
spring.datasource.driverClassName = com.mysql.jdbc.Driver
# Specify the DBMS
spring.jpa.database = MYSQL
# Show or not log for each sql query
spring.jpa.show-sql = true
# Hibernate ddl auto (create, create-drop, update)
spring.jpa.hibernate.ddl-auto = update
# Naming strategy
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
# stripped before adding them to the entity manager)
spring.jpa.properties.hibernate.dialect = org.hibernate.dial
㈥ 如何用springboot连接数据库
新建Spring Boot项目,依赖选择JPA(spring-boot-starter-data-jpa)和Web(spring-bootstarter-web)。
配置基本属性 在application.properties里配置数据源和jpa的相关属性
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/springboot
spring.datasource.username=root
spring.datasource.password=123456
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.jackson.serialization.indent_output=true
定义映射实体类
定义Controller类
@RestControllerpublic class PersonCtroller {
@Autowired PersonServer personServer;
@RequestMapping("/rollback")
public Person rollback(Person person){
return personServer.savePersonWithRollBack(person);
}
@RequestMapping("/norollback")
public Person noRollback(Person person){
return personServer.savePersonWithOutRollBack(person);
}
}
定义数据访问层
public interface PersonRepository extends JpaRepository<Person, Long> {}
定义Server层
@Servicepublic class PersonServerImp implements PersonServer {
@Autowired
PersonRepository personRepository;
@Transactional(rollbackFor = {IllegalArgumentException.class})
@Override
public Person savePersonWithRollBack(Person person) {
Person p = personRepository.save(person);
if (p.getName().equals("xxx")){
throw new IllegalArgumentException("用户已存在,数据会回滚");
}
return p;
}
}
7
浏览器访问