JAVAEE编程

  1. 创建mybatis数据库,并创建t_customer表。该表包含id(int)、username(varchar)、jobs(varchar)、phone(varchar)四个字段
public void mainTest() {
	    // 加载配置文件
	    ApplicationContext applicationContext = 
	            new ClassPathXmlApplicationContext("applicationContext.xml");
	    // 获取JdbcTemplate实例
	    JdbcTemplate jdTemplate = 
	            (JdbcTemplate) applicationContext.getBean("jdbcTemplate");
	    // 使用execute()方法执行SQL语句,创建用户账户管理表account
	    jdTemplate.execute("create table t_customer(" + 
	                           "id int primary key auto_increment," +
	                           "username varchar(50)," + 
" jobs varchar(50)," +
	                           " phone   varchar(50)");
	    System.out.println("账户表t_customer创建成功");

	}

2.编写数据库常量配置文件db.properties。(1分)

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/demo
jdbc.username=root
jdbc.password=123456
jdbc.maxTotal=30
jdbc.maxIdle=10
jdbc.initialSize=5

3.编写applicationContext.xml配置文件。(2分)

<?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/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
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
    <!-- 读取db.properties -->
    <context:property-placeholder location="classpath:db.properties"/>
    <!-- 配置数据源 -->
	<bean id="dataSource" 
                           class="org.apache.commons.dbcp2.BasicDataSource">
		<!--数据库驱动 -->
		<property name="driverClassName" value="${jdbc.driver}" />
		<!--连接数据库的url -->
		<property name="url" value="${jdbc.url}" />
		<!--连接数据库的用户名 -->
		<property name="username" value="${jdbc.username}" />
		<!--连接数据库的密码 -->
		<property name="password" value="${jdbc.password}" />
		<!--最大连接数 -->
		<property name="maxTotal" value="${jdbc.maxTotal}" />
		<!--最大空闲连接  -->
		<property name="maxIdle" value="${jdbc.maxIdle}" />
		<!--初始化连接数  -->
		<property name="initialSize" value="${jdbc.initialSize}" />
	</bean>
     <!-- 事务管理器,依赖于数据源 -->
	<bean id="transactionManager" class=
     "org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource" />
	</bean>	
    <!-- 开启事务注解 -->
	<tx:annotation-driven transaction-manager="transactionManager"/>
    <!-- 配置MyBatis工厂SqlSessionFactory -->
    <bean id="sqlSessionFactory" 
                           class="org.mybatis.spring.SqlSessionFactoryBean">
         <!--注入数据源 -->
         <property name="dataSource" ref="dataSource" />
         <!--指定核MyBatis心配置文件位置 -->
  <property name="configLocation" value="classpath:mybatis-config.xml" />
    </bean>
    <!-- 配置mapper扫描器 -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="wx.com.dao"/>
	</bean>
    <!-- 扫描Service --> 
    <context:component-scan base-package="wx.com.service" />
</beans>

4.编写mybatis-config.xml配置文件。(1分)

<?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="wx.com.po" />
	</typeAliases>
</configuration>

5.编写springmvc-config.xml配置文件。(2分)

<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xmlns:mvc="http://www.springframework.org/schema/mvc"
  xmlns:context="http://www.springframework.org/schema/context"
  xmlns:tx="http://www.springframework.org/schema/tx"
  xsi:schemaLocation="http://www.springframework.org/schema/beans 
  http://www.springframework.org/schema/beans/spring-beans-4.3.xsd 
  http://www.springframework.org/schema/mvc 
  http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd 
  http://www.springframework.org/schema/context 
  http://www.springframework.org/schema/context/spring-context-4.3.xsd">
	<!-- 配置包扫描器,扫描@Controller注解的类 -->
	<context:component-scan base-package="wx.com.controller" />
	<!-- 加载注解驱动 -->
	<mvc:annotation-driven />
	<!-- 配置视图解析器 -->
	<bean class=
    "org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/jsp/" />
		<property name="suffix" value=".jsp" />
	</bean>
</beans>

6.配置web.xml。(1分)

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
 
  	<!-- 配置加载Spring文件的监听器-->
	<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>
	<!-- 配置Spring MVC前端核心控制器 -->
	<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>
		<!-- 配置服务器启动后立即加载Spring MVC配置文件 -->
    	<!-- <load-on-startup>1</load-on-startup> -->
	</servlet>
	<servlet-mapping>
	<servlet-name>springmvc</servlet-name>
	<url-pattern>/</url-pattern>
	</servlet-mapping>
</web-app>

7.创建持久化类Customer.java。(1分)

package com.itheima.po;
/**
 * 客户持久化类
 */
public class Customer {
	private Integer id;       // 主键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;
	}
}

8.创建接口文件CustomerDao.java,定义添加客户的方法addCustomer和查询客户的方法findCustomer。(2分)

package com.itheima.dao;
import com.itheima.po.Customer;
/**
 * Customer接口文件
 */
public interface CustomerDao {
	/**
	 * 根据id查询客户信息
	 */
	public Customer findCustomer(Integer id);
public Customer addCustomer(Integer id);

}

9.创建CustomerDao.java对应的映射文件CustomerDao.xml,并定义相关的执行语句。(3分)

<?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">
	<!--根据id查询客户信息 -->
	<select id="findCustomerById" parameterType="Integer"
		                               resultType="Customer">
		select * from t_customer where id = #{id}
	</select>
		<insert id="addCustomer" parameterType="Customer">
		insert into t_customer (id,username,jobs,phone)
		values(#{id,jdbcType=Integer},#{username,jdbcType=VARCHAR},
		#{jobs,jdbcType=VARCHAR},#{phone,jdbcType=VARCHAR}
		)
	</insert>
</mapper>

10.在Service层创建接口文件CustomerService.java,并定义添加客户的方法

addCustomer和查询客户的方法findCustomer。(1分) 
package com.itheima.service;
import com.itheima.po.Customer;
public interface CustomerService {
	public Customer findCustomerById(Integer id);
	//插入Customer记录
	public int addCustomer(Customer customer);
}

11.创建CustomerService.java的实现类CustomerServiceImpl(1分)。

package com.itheima.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.itheima.dao.CustomerDao;
import com.itheima.po.Customer;
import com.itheima.service.CustomerService;
@Service
@Transactional
public class CustomerServiceImpl implements CustomerService {
	//注解注入CustomerDao
	@Autowired
	private CustomerDao customerDao;
	//查询客户
	public Customer findCustomer(Integer id) {
		return this.customerDao.findCustomerById(id);
	}
	//插入客户
	public int addCustomerr(Customer customer){
		int i = customerDai.addCustomer(customer);
		return i;
	}
}

12.在Controller层创建控制器类CustomerController。(2分)
package com.itheima.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import com.itheima.po.Customer;
import com.itheima.service.CustomerService;
@Controller
public class CustomerController {
	@Autowired
	private CustomerService customerService;
	/**
	 * 根据id查询客户详情
	 */
	@RequestMapping("/findCustomer")
	public String findCustomer(Integer id,Model model) {
		Customer customer = customerService.findCustomer(id);
		model.addAttribute("customer", customer);
		//返回客户信息展示页面
		return "customer";
	}

	@RequestMapping("/addCustomer")
	@ResponseBody
	public String addCustomer(Customer customer){
		int i = customerService.addCustomer(customer);
		if(i==1){
		return "添加成功";
		}else{
		return "添加失败";
		}
	}
}

13.编写显示查询客户结果的页面customer.jsp(1分)

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
    "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>客户信息</title>
</head>
<body>
	<table border=1>
		<tr>
			<td>编号</td>
			<td>名称</td>
			<td>职业</td>
			<td>电话</td>
		</tr>
		<tr>
			<td>${customer.id}</td>
			<td>${customer.username}</td>
			<td>${customer.jobs}</td>
			<td>${customer.phone}</td>
		</tr>
	</table>
</body>
</html>
  • 0
    点赞
  • 0
    评论
  • 0
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

©️2020 CSDN 皮肤主题: 数字20 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值