下面将为您详细讲解“Maven 搭建SpringMVC+Hibernate项目详解”的完整攻略:
1. 前置条件
- 已安装好Java JDK、Eclipse、Maven
- 已掌握基础的SpringMVC和Hibernate知识
2. 新建Maven项目
- 打开Eclipse,选择File -> New -> Other,选择Maven Project,然后点击Next
- 选择Create a simple project并勾选Create a simple project (skip archetype selection),然后点击Next
- 在Create a Maven Project界面,输入Group Id、Artifact Id和Version,并勾选Create a simple project (skip archetype selection),然后点击Finish
至此,一个Maven项目就创建完成了。
3. 添加SpringMVC依赖
在pom.xml中添加SpringMVC依赖:
<!-- SpringMVC -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.0.RELEASE</version>
</dependency>
添加该依赖之后,我们就可以编写SpringMVC控制器了。
示例1:
@Controller
public class HelloController {
@RequestMapping("/hello")
public String hello(Model model) {
model.addAttribute("name", "World");
return "hello";
}
}
4. 添加Hibernate依赖
在pom.xml中添加Hibernate依赖:
<!-- Hibernate -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.4.20.Final</version>
</dependency>
添加该依赖之后,我们就可以编写Hibernate实体类和DAO类了。
示例2:
@Entity
@Table(name = "user")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String name;
private String email;
// 省略getter和setter
}
@Repository
public class UserDaoImpl implements UserDao {
@Autowired
private SessionFactory sessionFactory;
public void saveOrUpdate(User user) {
Session session = sessionFactory.getCurrentSession();
session.saveOrUpdate(user);
}
public User get(int id) {
Session session = sessionFactory.getCurrentSession();
return session.get(User.class, id);
}
// 省略其他CRUD操作
}
5. 修改web.xml
在web.xml中添加DispatcherServlet的配置:
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
6. 添加SpringMVC配置文件
在src/main/resources目录下创建springmvc配置文件:springmvc-servlet.xml。
<context:component-scan base-package="com.example.controller" />
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
7. 添加Hibernate配置文件
在src/main/resources目录下创建Hibernate配置文件:hibernate.cfg.xml。
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
<property name="show_sql">true</property>
</session-factory>
</hibernate-configuration>
8. 示例代码
完整代码请参见:https://github.com/fuqihong/springmvc-demo
示例1:
@Controller
public class HelloController {
@RequestMapping("/hello")
public String hello(Model model) {
model.addAttribute("name", "World");
return "hello";
}
}
示例2:
@Entity
@Table(name = "user")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String name;
private String email;
// 省略getter和setter
}
@Repository
public class UserDaoImpl implements UserDao {
@Autowired
private SessionFactory sessionFactory;
public void saveOrUpdate(User user) {
Session session = sessionFactory.getCurrentSession();
session.saveOrUpdate(user);
}
public User get(int id) {
Session session = sessionFactory.getCurrentSession();
return session.get(User.class, id);
}
// 省略其他CRUD操作
}
以上就是“Maven 搭建SpringMVC+Hibernate项目详解”的完整攻略,希望能对你有所帮助。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:Maven 搭建SpringMVC+Hibernate项目详解 - Python技术站