我需要一些幫助來了解spring mvc中發生了什麼!比方說,我有一個顯示書籍的一個項目,我有:Spring mvc jdbc和hibernate
數據庫(MySQL的)的書籍,像這樣:
INSERT INTO books (title, author) VALUES ('Some book', 'this author')
的書籍也有一個自動遞增的ID(book_id)
在我們的項目,我們有一個書類:
@Entity
@Table
public class Books {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column
private int book_id;
private String title;
private String author;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column
public int getBook_id() {
return book_id;
}
public void setBook_id(int book_id) {
this.book_id = book_id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
我們有一個DAO
@Repository
public interface bookDao {
public List<Book> BookList();
}
而一個DaoImplementation
@Controller
@Transactional
public class BookDAOImpl implements BookDao {
@Inject
private SessionFactory sessionFactory;
private Session getSession(){
return sessionFactory.getCurrentSession();
}
@Override
public List<Book> bookList(){
return getSession().createQuery("from Book").list();
}
該處
@Service
@Transactional
public class BookService {
@Autowired
private BookDao bookDao;
public List<Book> list(){
return bookDao.bookList();
}
和控制器
@Controller
@RequestMapping("/")
public class HelloController {
@Autowired
BookService bookService;
@RequestMapping(value="/menu",method = RequestMethod.GET)
public String printMenu(){
return "menu";
}
@RequestMapping(value="/list", method = RequestMethod.GET)
public void list (Model model){
List<Book> books = bookService.list();
model.addAttribute("books", books);
}
}
最後,所述MVC-調度
<context:component-scan base-package="package"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"/>
<property name="suffix" value=".jsp"/>
</bean>
<util:properties id="hibernateProperties">
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.show_sql">false</prop>
</util:properties>
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource"
p:driverClassName="com.mysql.jdbc.Driver"
p:url="URL"
p:username="USERNAME"
p:password="PASSWORD"/>
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"
p:dataSource-ref="dataSource"
p:packagesToScan="package"
p:hibernateProperties-ref="hibernateProperties"/>
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"
p:sessionFactory-ref="sessionFactory"/>
我們還有.jsp頁面/菜單和/列表
當我進入本地主機:端口時,我將直接進入/菜單頁面,當我按下/ list時,出現什麼情況,它工作嗎?我希望有人能夠解釋這個過程,我很難理解'豆子'和'自動裝配',所以如果你知道,請分享!
和web.xml按請求!
<display-name>Spring MVC Application</display-name>
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
你忘了web.xml,從這裏開始。 – Jimmy 2014-12-03 17:34:01
我添加了web.xml,如果你想看看 – user3486059 2014-12-03 18:04:02