SpringMVC-HelloWorld – Annotation

工具

  1. Spring 3.2.13
  2. Maven 3
  3. JDK 1.6
  4. Tomcat 7
  5. Eclipse 4.4
  6. Boostrap 3

Project Structure

project structure
spring3-mvc HelloWorld project structure

Dependencies

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mkyong</groupId>
<artifactId>spring3-mvc-maven-annotation-hello-world</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>spring mvc</name>

<properties>
<jdk.version>1.6</jdk.version>
<spring.version>3.2.13.RELEASE</spring.version>
<jstl.version>1.2</jstl.version>
<servletapi.version>3.1.0</servletapi.version>
</properties>

<dependencies>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>

<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>${jstl.version}</version>
</dependency>

<!-- compile only, deployed container will provide this -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>${servletapi.version}</version>
<scope>provided</scope>
</dependency>

</dependencies>

<build>
<plugins>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>${jdk.version}</source>
<target>${jdk.version}</target>
</configuration>
</plugin>

<!-- embedded Jetty server, for testing -->
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.2.11.v20150529</version>
<configuration>
<scanIntervalSeconds>10</scanIntervalSeconds>
<webApp>
<contextPath>/spring3</contextPath>
</webApp>
</configuration>
</plugin>

<!-- configure Eclipse workspace -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.9</version>
<configuration>
<downloadSources>true</downloadSources>
<downloadJavadocs>true</downloadJavadocs>
<wtpversion>2.0</wtpversion>
<wtpContextName>spring3</wtpContextName>
</configuration>
</plugin>

</plugins>
</build>

</project>

Java Code

SpringWebConfig.java

配置Spring Bean,代替applicationContext.xml配置文件

package com.mkyong.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;

@EnableWebMvc //mvc:annotation-driven
@Configuration
@ComponentScan({ "com.mkyong.web" })
public class SpringWebConfig extends WebMvcConfigurerAdapter {

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
}

@Bean
public InternalResourceViewResolver viewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setViewClass(JstlView.class);
viewResolver.setPrefix("/WEB-INF/views/jsp/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}

}

MyWebInitializer.java

初始化并启动Spring,代替web.xml

package com.mkyong.servlet3;

import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

import com.mkyong.config.SpringWebConfig;

public class MyWebInitializer extends
AbstractAnnotationConfigDispatcherServletInitializer {

@Override
protected Class<?>[] getServletConfigClasses() {
return new Class[] { SpringWebConfig.class };
}

@Override
protected String[] getServletMappings() {
return new String[] { "/" };
}

@Override
protected Class<?>[] getRootConfigClasses() {
return null;
}

}

HelloController.java

package com.mkyong.web.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class HelloController {

@RequestMapping(value = "/", method = RequestMethod.GET)
public String printWelcome(ModelMap model) {

model.addAttribute("message", "Spring 3 MVC Hello World");
return "hello";

}

@RequestMapping(value = "/hello/{name:.+}", method = RequestMethod.GET)
public ModelAndView hello(@PathVariable("name") String name) {

ModelAndView model = new ModelAndView();
model.setViewName("hello");
model.addObject("msg", name);

return model;

}

}

Hibernate进阶之路

Content

1.配置与SessionFactory

2.Mapping

2.1.Mapping File

2.2.Associations

2.2.1.Many-To-Many

2.2.2.One-To-Many

2.2.3.unidirectional link 和 bidirectional link

1.配置与SessionFactory

1.1.hibernate.cfg.xml

1.2.Programmatic配置

  • Configuration.addResource(),使用*.hbm.xml配置
    Configuration cfg = new Configuration()
    .addResource("Item.hbm.xml")
    .addResource("Bid.hbm.xml");
  • Configruration.addClass(),根据类名ClassName查找类所在包下的ClassName.hbm.xml
    Configuration cfg = new Configuration()
    .addClass(org.hibernate.auction.Item.class)
    .addClass(org.hibernate.auction.Bid.class);
  • Configuration.setProperty()
    Configuration cfg = new Configuration()
    .setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLInnoDBDialect")
    .setProperty("hibernate.connection.datasource", "java:comp/env/jdbc/test")
    .setProperty("hibernate.order_updates", "true");

    使用properties的方法:

    • 使用java.util.Properties作为参数调用Configuration.setProperties().
    • classpath的根目录下hibernate.properties文件
    • hibernate.cfg.xml中元素

2.Mapping

2.1.Mapping File

2.2.Associations

2.3.unidirectional link 和 bidirectional link

Hibernate进阶之路—-Hibernate API

目录

1.SessionFactory

1.SessionFactory (org.hibernate.SessionFactory)

1.1.overview

  • thread-safe
  • 单数据库
  • 保存编译后mappings,不可变
  • org.hibernate.Session的工厂
  • org.hibernate.connection.ConnectionProvider的客户类

1.2.创建

Configuration configuration = new Configuration();
configuration.configure();
SessionFactory sessionFactory = configuration.buildSessionFactory();

可以为每个数据库创建一个SessionFactory对象
SessionFactory对象创建后Configuration对象将被丢弃

Session (org.hibernate.Session)

  • 单线程
  • short-lived object
  • 应用程序和持久层间的会话
  • JDBC java.sql.Connection的包装
  • org.hibernate.Transaction的工厂
  • 维护应用程序和持久层的一级cache

Persistent objects and collections

  • JavaBean/POJO
  • Short-lived
  • 单线程
  • 只和一个org.hibernate.Session关联?

Transient and detached objects and collections

  • 未和Session关联的持久化类的对象

Transaction (org.hibernate.Transaction)

  • single-threaded
  • short-lived object
  • JDBC, JTA or CORBA transaction的抽象

ConnectionProvider (org.hibernate.connection.ConnectionProvider)

  • JDBC connections的工厂及池
  • DataSource或DriverManager的抽象

TransactionFactory (org.hibernate.TransactionFactory)

  • org.hibernate.Transaction的工厂
  • 不暴露给application, 可extend 或 implements

Extension Interfaces