@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("home");
}
}
跟这个代码对应的XML配置代码如下:
<mvc:view-controller path="/" view-name="home"/>
@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
}
同样的功能可使用如下XML代码实现:
<mvc:default-servlet-handler/>
@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new LocaleInterceptor());
registry.addInterceptor(new ThemeInterceptor()).addPathPatterns("/**").excludePathPatterns("/admin/**");
registry.addInterceptor(new SecurityInterceptor()).addPathPatterns("/secure/*");
}
}
对应的XML配置代码如下:
<mvc:interceptors>
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"/>
<mvc:interceptor>
<mvc:mapping path="/**"/>
<mvc:exclude-mapping path="/admin/**"/>
<bean class="org.springframework.web.servlet.theme.ThemeChangeInterceptor"/>
</mvc:interceptor>
<mvc:interceptor>
<mvc:mapping path="/secure/*"/>
<bean class="org.example.SecurityInterceptor"/>
</mvc:interceptor>
</mvc:interceptors>
这个配置的含义有二:(1)对于所有符合"/"模式的请求(除"/admin/"之外)要应用ThemeChangeInterceptor拦截器;(2)对于所有符合"/secure/*"模式的请求,都要应用SecurityInterceptor拦截器。
public class ConstellationBuilder {
public static String getAstro(int month, int day) {
String[] starArr = {"魔羯座", "水瓶座", "双鱼座", "牡羊座", "金牛座", "双子座", "巨蟹座", "狮子座",
"处女座", "天秤座", "天蝎座", "射手座"};
int[] DayArr = {22, 20, 19, 21, 21, 21, 22, 23, 23, 23, 23, 22}; //两个星座分割日
int index = month; // 所查询日期在分割日之前,索引-1,否则不变
if (day < DayArr[month - 1]) {
index = index - 1;
}
return starArr[index];
}
}
对应的测试代码如下,注意Joda Time包的使用,不过如果你使用Java 8的话,可以直接使用Java 8提供的日期接口。
public class ConstellationBuilderTest {
@Test
public void getAstro() throws Exception {
Calendar calendar = Calendar.getInstance();
calendar.set(1988, Calendar.OCTOBER, 16);
LocalDateTime localDateTime = LocalDateTime.fromDateFields(calendar.getTime());
String res = ConstellationBuilder.getAstro(localDateTime.getMonthOfYear(), localDateTime.getDayOfMonth());
Assert.assertEquals("天秤座", res);
}
}
<insert id="insertOrUpdate" parameterType="userBean">
<![CDATA[
INSERT INTO
user
(id, name, sex, birthday, constellation, work, province, city, district, mobile, integral, ctime, mtime)
VALUES (#{id}, #{name}, #{sex}, #{birthday}, #{constellation}, #{work}, #{province}, #{city}, #{district},
#{mobile}, #{integral}, now(), now())
ON DUPLICATE KEY
UPDATE
name=VALUES(name),
sex=VALUES(sex),
birthday=VALUES(birthday),
constellation=VALUES(constellation),
work=VALUES(work),
province=VALUES(province),
city=VALUES(city),
district=VALUES(district),
mobile=VALUES(mobile),
integral=VALUES(integral),
mtime=now()
]]>
</insert>
MySQL的官方文档,要找个时间认真阅读,有问题的时候,也可以直接翻阅官方文档,而不是漫无目的得Google。根据官方文档-insert-on-duplicate中提到:
You can use the VALUES(col_name) function in the UPDATE clause to refer to column values from the INSERT portion of the INSERT ... ON DUPLICATE KEY UPDATE statement. In other words, VALUES(col_name) in the ON DUPLICATE KEY UPDATE clause refers to the value of col_name that would be inserted, had no duplicate-key conflict occurred. This function is especially useful in multiple-row inserts. The VALUES() function is meaningful only in INSERT ... UPDATE statements and returns NULL otherwise. 重点:VALUES函数用于提取对应的列值,如果没有则返回NULL;
public class Runtime {
private static Runtime currentRuntime = new Runtime();
/**
* Returns the runtime object associated with the current Java application.
* Most of the methods of class <code>Runtime</code> are instance
* methods and must be invoked with respect to the current runtime object.
*
* @return the <code>Runtime</code> object associated with the current
* Java application.
*/
public static Runtime getRuntime() {
return currentRuntime;
}
/** Don't let anyone else instantiate this class */
private Runtime() {}
}
原有的旧方法,在反序列号方面会有问题,需要我们重写方法,在Java 1.7之后,实现单利模式的最佳实践是利用枚举(枚举类型天然支持序列化)。参考StackOverflow:Implementing Singleton with an Enum (in Java)
// Enum singleton - the preferred approach
public enum Elvis {
INSTANCE;
public void leaveTheBuilding() { ... }
}