控制器:
@Controller
@RequestMapping("/general")
public class OrderController {
private OrderService orderService;
@Autowired
@Qualifier("orderServiceImpl")
public void setOrderService(OrderService orderService) {
this.orderService = orderService;
}
@GetMapping(value = "/list")
public String get(Model theModel) {
theModel.addAttribute("orders", orderService.getOrders());
return "index";
}
@GetMapping(value = "/showFormForAdd")
public String add(Model theModel) {
Orders orders = new Orders();
theModel.addAttribute("order", orders);
return "create-orders";
}
@PostMapping(value = "/saveOrders")
public String addOrders(@ModelAttribute Orders orders, @RequestParam MultipartFile file) throws IOException {
orders = orderService.uploadOrders(orders, file);
orderService.save(orders);
return "redirect:/general/list";
}
}
型号:
@Entity
@Table(name = "orders")
public class Orders {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private int id;
@Column(name = "description")
private String description;
@Column(name = "price")
private int price;
@Column(name = "sold")
private boolean sold;
@Column(name = "body")
private String body;
@Column(name = "brand")
private String brand;
@Column(name = "engine")
private String engine;
@Column(name = "model")
private String model;
@Lob
@Column(name = "image")
private byte[] image;
@Column(name = "imageStr")
private String imageStr;
public Orders() {
}
public Orders(int id) {
this.id = id;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public boolean isSold() {
return sold;
}
public void setSold(boolean sold) {
this.sold = sold;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public String getBody() {
return body;
}
public void setBody(String body) {
this.body = body;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public String getEngine() {
return engine;
}
public void setEngine(String engine) {
this.engine = engine;
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
public byte[] getImage() {
return image;
}
public void setImage(byte[] image) {
this.image = image;
}
public String getImageStr() {
return imageStr;
}
public void setImageStr(String imageStr) {
this.imageStr = imageStr;
}
}
存储库:
@Repository
public interface Dao extends CrudRepository<Orders, Long>{
}
public interface OrderService {
List<Orders> getOrders();
Orders uploadOrders(Orders orders, MultipartFile file) throws IOException;
void save(Orders orders);
}
@Service
public class OrderServiceImpl implements OrderService {
private Dao dao;
@Autowired
public OrderServiceImpl(Dao dao) {
this.dao = dao;
}
@Override
public List<Orders> getOrders() {
return Lists.newArrayList(dao.findAll());
}
@Override
public void save(Orders orders) {
dao.save(orders);
}
@Override
public Orders uploadOrders(Orders orders, MultipartFile file) throws IOException {
if (!file.isEmpty()) {
String uploadDir = file.getOriginalFilename();
orders.setImage(file.getBytes());
orders.setImageStr(uploadDir);
}
return orders;
}
}
<?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:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context-4.0.xsd">
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
<import resource="hibernate.cfg.xml"/>
<bean id = "dao" class ="repository.Dao" abstract="true"/>
<context:component-scan
base-package="
controller
repository
service">
</context:component-scan>
</beans>
<?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:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<bean id="myDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.postgresql.Driver"/>
<property name="url"/>
<property name="username" value="postgres"/>
<property name="password" value="root"/>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="myDataSource"/>
<property name="packagesToScan" value="models"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQL9Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<bean id="myTransactionManager"
class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<tx:annotation-driven transaction-manager="myTransactionManager"/>
</beans>
web.xml
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring-context.xml
</param-value>
</context-param>
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
我在hibernate.cfg.xml中添加了如下代码:
<bean id="myDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.postgresql.Driver"/>
<property name="url"/>
<property name="username" value="postgres"/>
<property name="password" value="root"/>
</bean>
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="packagesToScan" value="models" />
<property name="dataSource" ref="myDataSource" />
<property name="jpaProperties">
<props>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
</props>
</property>
<property name="persistenceProvider">
<bean class="org.hibernate.jpa.HibernatePersistenceProvider"></bean>
</property>
</bean>
<bean class="org.springframework.orm.jpa.JpaTransactionManager" id="transactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<jpa:repositories base-package="repository" entity-manager-factory-ref="entityManagerFactory" />
此外,我还在OrderServiceImpl类中添加了以下内容:
@Resource
private Dao dao;
现在没有错误,一切正常
我正在使用Spring Boot 2.2.4并尝试自定义执行器 以下是相关的课程
我想在xml JDBCTemolate上进行配置。 看起来是这样的 JDBCrepository: 控制器:
我尝试构建简单的Maven springMVC应用程序。当我试图将应用程序部署到我的服务器时,下面抛出了异常。 unsatisfiedDependencyException:创建com.phapp.comfiguration.webconfiguration中定义的名为“View Resolver”的bean时出错:通过方法“View Resolver”参数0表示未满足的依赖关系;嵌套异常是org
当试图用包含所有上下文配置的抽象类运行stepdefs时,spring看到2个不同的beans parent和step def 我使用的是Spring Booking版本:2.6.4,JUnit 5和Cucumber版本7.2.3 异常堆栈跟踪: io.cucumber.core.runtime.CucumberExecutionContext.runTestCase:没有可用的“Cucumber
我尝试自动连接我的mapstruct mapper: 这是可行的: 但是为什么我不能使用: 我得到以下错误: 导致原因:org . spring framework . beans . factory . nosuchbeandidefinitionexception:没有类型为“pl . comp . window . application . mapper . windowdtomapper