Spring 整合异常
- spring 整合hibernate注解时候,出现“Unknown entity: com.ssh.entry.Admin; nested exception is org.hibernate.MappingException: Unknown entity: com.ssh.entry.Admin
- 使用Spring和MyBatis缺少@Transactional时引发异常
- Can i configure the Spring MVC application so that when ever i run the application, the controller or any method should be called automatically
- Spring中的@ Component,@ Repository和@Service批注有什么区别?
spring 整合hibernate注解时候,出现“Unknown entity: com.ssh.entry.Admin; nested exception is org.hibernate.MappingException: Unknown entity: com.ssh.entry.Admin
今天学习使用ssh框架的时候,出现一个异常,弄了好久才找到,在这记录一下,我的sb错误
1.spring整合hibernate,取代*.hbm.xml配置文件
在applicationContext.xml文件中配置方式
`<!-- 数据库连接池 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="jdbcUrl" value="jdbc:mysql:///databaseName"/> <property name="driverClass" value="com.mysql.jdbc.Driver"/> <property name="user" value="UserName"/> <property name="password" value="pwd"/> </bean> <!-- sessionFactory --> <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"> <!-- 引入数据库连接池 --> <property name="dataSource" ref="dataSource"/><!-- 引入hibernate映射文件 --> <property name="mappingresources"> <list> <value>com/abc/def/*.hbm.xml</value> </list> </property><!-- 配置数据库常用参数 --> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">true</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> </props> </property> </bean>`
spring整合hibernate,使用注解的方式
在applicationContext.xml文件中配置方式
`<!-- 数据库连接池 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="jdbcUrl" value="jdbc:mysql:///databaseName"/> <property name="driverClass" value="com.mysql.jdbc.Driver"/> <property name="user" value="UserName"/> <property name="password" value="pwd"/> </bean> <!-- sessionFactory --> <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"> <!-- 引入数据库连接池 --> <property name="dataSource" ref="dataSource"/><!-- 引入hibernate映射文件 --> <property name="packagesToScan"> <list> <value>com.ssh.entry</value> </list> </property><!-- 配置数据库常用参数 --> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">true</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> </props> </property> </bean>`
两种方式只有在引入映射文件或者注解类时候有差别
我在第一次整合注解时候,出现问题:
message Unknown entity: com.ssh.entry.Admin; nested exception is org.hibernate.MappingException: Unknown entity: com.ssh.entry.Admin description The server encountered an internal error that prevented it from fulfilling this request.
是因为我在引入注解类所在包的时候,把 * <value>com.ssh.entry</value> * 的值精确到了类名,导致错误,应该只写到包名就可以了
<property name="packagesToScan"> <list> <value>com.ssh.entry</value> </list>
https://developer.aliyun.com/article/633370
使用Spring和MyBatis缺少@Transactional时引发异常
我正在尝试设置使用Spring和MyBatis的Web应用程序。
以下是重要的代码片段。
Maven依赖项pom.xml
:
...
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.3.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.3.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.4.1212.jre7</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.2</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.1</version>
</dependency>
...
和配置Spring bean:
@Configuration
@EnableTransactionManagement
public class DatabaseConfiguration {
@Value("classpath:db/mybatis/mybatis-configuration.xml")
private Resource myBatisConfiguration;
@Bean
public DataSource dataSource() {
final DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("org.postgresql.Driver");
dataSource.setUrl("jdbc:postgresql://127.0.0.1:5432/ehdb");
dataSource.setUsername(/*my username*/);
dataSource.setPassword(/*my password*/);
return dataSource;
}
@Bean
public PlatformTransactionManager transactionManager() {
final DataSourceTransactionManager transactionManager = new DataSourceTransactionManager(dataSource());
transactionManager.setValidateExistingTransaction(true);
return transactionManager;
}
@Bean
public SqlSessionFactoryBean sqlSessionFactoryBean() {
final SqlSessionFactoryBean sqlSessionFactory = new SqlSessionFactoryBean();
sqlSessionFactory.setDataSource(dataSource());
sqlSessionFactory.setConfigLocation(myBatisConfiguration);
return sqlSessionFactory;
}
@Bean
public SqlSession sqlSession() throws Exception {
return new SqlSessionTemplate(sqlSessionFactoryBean().getObject());
}
}
这是一个应该在事务方法中调用一些MyBatis语句的服务:
@Service
public class HelloManagerImpl implements HelloManager {
private final HelloDao helloDao;
public HelloManagerImpl(@Autowired final HelloDao helloDao) {
this.helloDao = helloDao;
}
@Override
@Transactional
public String doSomething() {
helloDao.insertRow(); // a row is inserted into DB table via MyBatis; bean sqlSession is autowired in HelloDao
throw new RuntimeException(); // transaction will be rolled back here
}
}
如果我调用该方法doSomething
,它将按预期工作。由于抛出了事务,因此回滚了事务,因此数据库表中没有新行出现RuntimeException
。
如果我注释掉throw语句并重复实验,则数据库表中会出现新的一行。这又是预期的行为。
现在,如果我另外注释掉了@Transactional
注释并调用doSomething()
,则该方法成功,并将新行插入到表中。INSERT
如果没有事务存在,MyBatis似乎会自动为该语句创建事务。
我宁愿在最后一种情况下失败。如果我忘了写@Transactional
注释,那可能是一个错误。如果在这种情况下引发异常,迫使我修复代码,而不是默默地创建一些事务,那会很好。
有没有办法做到这一点?
感谢帮助。
我建议将所有事务设置为只读,以实现最后一种情况,并且仅注释应在数据库中写入的方法。例如,您的服务如下所示:
@Service
@Transactional(readOnly = true)
public class HelloManagerImpl implements HelloManager {
private final HelloDao helloDao;
public HelloManagerImpl(@Autowired final HelloDao helloDao) {
this.helloDao = helloDao;
}
@Override
@Transactional(readOnly = false)
public String doSomething() {
helloDao.insertRow(); // a row is inserted into DB table via MyBatis; bean sqlSession is autowired in HelloDao
throw new RuntimeException(); // transaction will be rolled back here
}
}
-
+1这不是我最初寻找的MyBatis配置,但这是一个合理的解决方法。我也在考虑
@Transactional(propagation = Propagation.MANDATORY)
DAO类或方法。 – Cimlman 17年 1月27日在9:57
https://stackoverflow.com/questions/41768089/throw-exception-on-missing-transactional-with-spring-and-mybatis
Can i configure the Spring MVC application so that when ever i run the application, the controller or any method should be called automatically
I have developed a spring application with rest controller which will have only one GET method. Can i configure the Spring MVC application so that when ever i run the application, the controller or any method should be called automatically with out hitting the api from browser(Just like stand alone application)
You can create a bean that implements org.springframework.boot.CommandLineRunner
interface.
E.g.
@Component
public class MyRunner implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
// Do stuff here at startup
}
}
As this is a regular Spring component, you can use field or constructor injection to inject other Spring components.
https://stackoverflow.com/questions/64371317/can-i-configure-the-spring-mvc-application-so-that-when-ever-i-run-the-applicati
MYISAM
引擎没有。 – 空白 '17一月24'在1:27