JavaEE项目实现
1.在项目的src目录下创建log4j.properties文件。
log4j.rootLogger=ERROR, stdout
log4j.logger.com.itheima=DEBUG
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
2.在src目录下创建db.properties 文件。编写数据库相关属性和对应值。
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis
jdbc.username=root
jdbc.password=123456
jdbc.maxTotal=30
jdbc.maxIdle=10
jdbc.initialSize=5
3.在src目录下,创建Spring的配置文件applicationContext.xml.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
" >
<context:property-placeholder location="classpath:db.properties"/>
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
<property name="driverClassName" value="${jdbc.driver}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="maxTotal" value="${jdbc.maxTotal}"></property>
<property name="maxIdle" value="${jdbc.maxIdle}"></property>
<property name="initialSize" value="${jdbc.initialSize}"></property>
</bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.itheima.dao"></property>
</bean>
<context:component-scan base-package="com.itheima.service"></context:component-scan>
</beans>
4.在src目录下,创建MyBatis的核心配置文件mybatis-config.xml.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<typeAliases>
<package name="com.itheima.po"/>
</typeAliases>
</configuration
5.src目录下,创建SpringMVC的配置文件springmvc-config.xml,声明中引入spring-context,使用context:component-scan元素指定需要的类包。
<?xml version="1.0" encoding="UTF-8"?>
<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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
">
<context:component-scan base-package="com.itheima.controller"></context:component-scan>
</beans>
6.修改web.xml,在文件配置SpringMVC前端控制器DispatcherServlet.
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc-config.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
7.在src目录下,创建一个com.itheima.po包,在该包下创建持久化类Customer,编写属性和属性的set/get方法。
package com.itheima.po;
public class Customer {
private Integer id;
private String username;
private String jobs;
private String phone;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getJobs() {
return jobs;
}
public void setJobs(String jobs) {
this.jobs = jobs;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
@Override
public String toString() {
return "Customer [id=" + id + ", username=" + username + ", jobs=" + jobs + ", phone=" + phone + "]";
}
}
8.在src目录下,创建一个com.itheima.dao包,并在包中创建接口CustomerDao和CustomerDao.xml。在CustomerDao.java中编写public Customer findCustomerById(Integer id)抽象方法,在CustomerDao.xml中编写实现该方法的SQL语句
package com.itheima.dao;
import com.itheima.po.Customer;
public interface CustomerDao {
public Customer findCustomerById(Integer id);
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.itheima.dao.CustomerDao">
<select id="findCustomerById" parameterType="Integer" resultType="customer">
select * from t_customer where id=#{id}
</select>
</mapper>
9.在src目录下,创建一个com.itheima.service包,并在包中创建接口CustomerService和CustomerServiceImp实现类。在CustomerService.java中编写public Customer findCustomerById(Integer id)抽象方法,在CustomerServiceImp.java中实现该抽象方法
public interface CustomerService{
public Customer findCustomerById(Integer id);
}
CustomerServiceImp.java
@Service
public class CustomerServiceImp implements CustomerService {
@Autowired
private CustomerDao customerDao;
public Customer findCustomerById(Integer id) {
return this.customerDao.findCustomerById(id);
}
}
10.在src目录下,创建一个com.itheima.controller包,在包下创建CustomerController类,在类中添加到findCustomerById方法,并通过@RequestMapping注解该方
@Controller
public class CustomerController {
@Autowired
private CustomerService customerService;
@RequestMapping("/findCustomerById")
public String findCustomerById(Integer id,Model model) {
Customer customer =customerService.findCustomerById(id);
model.addAttribute("customer", customer);
return "customer";
}
}
11在Web-INF目录下,创建名为jsp的文件夹,然后在文件中创建customer.jsp页面。
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="=UTF-8">
<title>添加用户</title>
</head>
<body>
<table border="1">
<tr>
<td>编号</td>
<td>名称</td>
<td>职业</td>
<td>电话</td>
</tr>
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异