简体   繁体   中英

Spring mvc jdbc and hibernate

I need some help to understand what is going on in spring mvc! Let's say I have a project that displays books, I have:

A database (mysql) with books, like so:

INSERT INTO books (title, author) VALUES ('Some book', 'this author')

The books also have an auto incremented id (book_id)

In our project we have a book class:

@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;
}

We have a DAO

@Repository
public interface bookDao {
public List<Book> BookList();
}

And a 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();
}

The Service

@Service
@Transactional
public class BookService {

@Autowired
private BookDao bookDao;

public List<Book> list(){
    return bookDao.bookList();
}

and the Controller

@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);
}
}

And lastly, the mvc-dispatcher

    <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"/>

We also have the .jsp pages /menu and /list

And when I go to localhost:port I will be directed to the /menu page, and when I press go to /list, what happends, and how does it work? I hope someone can explain the process, I have a hard time understanding 'beans' and 'autowire', so please if you know, share!

and the web.xml as per request!

    <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>

It's easy to follow, but this is the Spring basics you are asking, so reading a nice tutorial or a Spring book would be the desired effort on your part...

Spring MVC is built on top of Servlet techniques. The DispatcherServlet is your most important entry point when dealing with Spring. It is in charge or accepting and distributing your requests coming into your Spring web app.

The DispatcherServlet (which config is usually in the web.xml ) receives a request for you HelloController . Note, how you have a @RequestMapping("/") above the method. This points to the root context path of your web app. So, hitting localhost:port will route the request to this particular Controller (via the DispatcherServlet ).

By the same token, by pointing to /list , your request would be routed to that particular method.

@Autowired is an annotation that tells the Spring container to initialize that particular bean (POJO), resolve dependencies and then provide/inject it into your resource for usage. This is called dependency injection or DI and if you are using Spring, you SHOULD know what it is and why we use it.

The file you called mvc-dispatcher is how you configure the Spring container or context. A bean is a simple class which, when defined there WILL BE managed by the Spring container. By manage i mean: initialize, resolve dependencies and inject when needed.

I could go on forever, but this should get your started...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM