从MyEclipse转IDEA后发现它功能真的很强大啊,首先列举一下目前发现的比较好用的功能吧,刚刚使用一周,其他强大功能有待发现。
界面简洁,漂亮,功能一目了然,界面风格跟Android Studio,PHPStrom差不多
主题风格较多,更加绚丽多彩
强大的插件功能,各种插件啊,有强大的插件库支持
Maven支持很方便,感觉比Eclipse好用
Spring,hibernate,Jpa,数据库支持强大
JRebel热部署功能很实用
<properties>
<spring.version>4.2.0.RELEASEspring.version>
<spring-data.version>1.2.0.RELEASEspring-data.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-coreartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>servlet-apiartifactId>
<version>2.5version>
dependency>
<dependency>
<groupId>javax.servlet.jspgroupId>
<artifactId>jsp-apiartifactId>
<version>2.1version>
<scope>providedscope>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webmvcartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-testartifactId>
<version>${spring.version}version>
<scope>testscope>
dependency>
<dependency>
<groupId>jstlgroupId>
<artifactId>jstlartifactId>
<version>1.2version>
dependency>
<dependency>
<groupId>org.springframework.datagroupId>
<artifactId>spring-data-jpaartifactId>
<version>${spring-data.version}version>
dependency>
<dependency>
<groupId>org.hibernate.javax.persistencegroupId>
<artifactId>hibernate-jpa-2.0-apiartifactId>
<version>1.0.0.Finalversion>
dependency>
<dependency>
<groupId>org.hibernategroupId>
<artifactId>hibernate-entitymanagerartifactId>
<version>3.6.10.Finalversion>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>5.1.34version>
dependency>
<dependency>
<groupId>org.jsongroupId>
<artifactId>jsonartifactId>
<version>20080701version>
dependency>
<dependency>
<groupId>com.fasterxml.jackson.coregroupId>
<artifactId>jackson-databindartifactId>
<version>2.7.2version>
dependency>
dependencies>
这里面需要注意:
3.配置SpringMvc的web.xml文件
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<display-name>SpringMVCDemo Web Applicationdisplay-name>
<servlet>
<servlet-name>mvc-dispatcherservlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
<load-on-startup>1load-on-startup>
servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcherservlet-name>
<url-pattern>/url-pattern>
servlet-mapping>
<filter>
<filter-name>encodingFilterfilter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
<init-param>
<param-name>encodingparam-name>
<param-value>UTF-8param-value>
init-param>
<init-param>
<param-name>forceEncodingparam-name>
<param-value>trueparam-value>
init-param>
filter>
<filter-mapping>
<filter-name>encodingFilterfilter-name>
<url-pattern>/*url-pattern>
filter-mapping>
web-app>
4.添加jpa配置
使用IntelliJ工具创建jpa支持
右键点击项目
使用数据库表添加对应的jpa实体类
添加数据库连接,以及表之间的关联关系
完整的persistence.xml文件
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">
<persistence-unit name="NewPersistenceUnit" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistenceprovider>
<class>com.palmlink.model.UserEntityclass>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>
<property name="hibernate.connection.url"
value="jdbc:mysql://localhost:3306/springdemo"/>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
<property name="hibernate.connection.username" value="root"/>
<property name="hibernate.connection.password" value=""/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.connection.useUnicode" value="true"/>
<property name="hibernate.connection.characterEncoding" value="UTF-8"/>
<property name="hibernate.format_sql" value="true"/>
<property name="hibernate.use_sql_comments" value="false"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
<property name="hibernate.connection.autoReconnect" value="true"/>
<property name="connection.autoReconnectForPools" value="true"/>
<property name="connection.is-connection-validation-required" value="true"/>
properties>
persistence-unit>
persistence>
5.配置dispatch- servlet.xml文件
在web的同级目录下新建一个servlet-name对应的xml文件,这里为mvc-dispatch- servlet.xml文件。
<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:mvc="http://www.springframework.org/schema/mvc"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<context:component-scan base-package="com.palmlink.controller"/>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="mappingJackson2HttpMessageConverter" />
list>
property>
bean>
<bean id="mappingJackson2HttpMessageConverter"
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8value>
<value>text/json;charset=UTF-8value>
<value>application/json;charset=UTF-8value>
list>
property>
bean>
<mvc:default-servlet-handler/>
<mvc:annotation-driven/>
<bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/pages/"/>
<property name="suffix" value=".jsp"/>
bean>
<jpa:repositories base-package="com.palmlink.repository"/>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
<property name="persistenceUnitName" value="NewPersistenceUnit"/>
bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
beans>
在配置servlet文件的时候需要添加Controller, Repository文件的映射。先在src/main目录下建一个java目录,Mark as root directory。之后添加package,再分别创建controller、repository、model目录。
创建一个Control Class文件,添加@Controller注解,告诉框架这是一个控制器文件。
下面是一个简单的http Get请求操作,对应的url为localhost:8080/admin/user,http访问方式为Get。这个method,默认为GET、Post两种
@RequestMapping(value = "/admin/user",method = RequestMethod.GET)
public String userlist(){
return "admin/user";
}
下面是一个保存网页表单内容到数据库的一个操作,使用@ModelAttribute(“user”)可以将表单提交上来的各个字段封装为一个指定的实体类。是不是很方便
@RequestMapping(value = "/admin/user/addAction",method = RequestMethod.POST)
public String userAddAction(@ModelAttribute("user")UserEntity userEntity){
userEntity.setPassword(Tool.md5(userEntity.getPassword()));
userRepository.save(userEntity);
return "admin/userAdd";
}
下面是通过id删除数据库表中对应数据的一个操作,使用@PathVariable(“id”) 可以获取从前台http传过来的字段名为id的值。然后直接使用。spring配置文件中配置过json返回后,使用@ResponseBody可以返回一个json字符串,内部自动将一个实体实例直接转换为对应的json字符串。这点真的很强大,很方便。
@RequestMapping(value = "/admin/users/delete/{id}")
@ResponseBody
public MessageJson deleteUserPost(@PathVariable("id") Integer id){
MessageJson messageJson = new MessageJson();
userRepository.delete(id);
userRepository.flush();
messageJson.setSuccess(true);
messageJson.setMsg("删除成功");
return messageJson;
}
通过面板工具直接创建,需要选择表,字段,添加对应的关联关系,然后自动生成。
在repository目录下创建一个类,继承JpaRepository
public interface JpaRepository<T, ID extends Serializable> extends PagingAndSortingRepository<T, ID>
Repository里面包含了增删查改对应的函数以及分页函数
@RequestMapping(value = "/admin/user",method = RequestMethod.GET)
public String userlist(){
return "admin/user";
}
@RequestMapping(value = "/admin/user/data")
@ResponseBody
public DataTable userPageData(HttpServletRequest request){
int size =Integer.parseInt(request.getParameter("limit"));
int page = Integer.parseInt(request.getParameter("page"));
int offset = (page-1)*size; System.out.println("offset="+offset+",size="+size+",page="+page);
PageRequest pageRequest = new PageRequest(page-1,size);
Page pageData = userRepository.findAll(pageRequest);
List list = pageData.getContent();
DataTable dataTableJson = new DataTable();
dataTableJson.setTotalCount(pageData.getTotalElements());
dataTableJson.setItems(list);
return dataTableJson;
}
使用HttpServletRequest request对象也可以获取服务器传来的参数。使用jpa的find(Page)函数可以直接返回一个分页数据。
这是我写的第一个CSDN博客,可能文章排版、文字表述、条例上有点混乱,写的不好的地方提出了改进,不喜勿喷。