发表于: 2017-09-20 22:18:22
1 704
一、今日完成
1.根据web学员任务9做的修真院静态主页,设计后端接口,昨天设计了数据库以及学习JSP标签用法,今天使用Spring MVC框架创建web项目;
1)使用注解定义Bean
@Repository //通过Spring 注解定义一个DAO
public class StudentDaoImpl implements StudentDao {
private JdbcTemplate jdbcTemplate;
@Autowired//注入JdbcTemplate的Bean
public void setJdbcTemplate (JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
@Service
public class StuServiceImpl implements StuService {
private StudentDao stuDao;
@Autowired
public void setStDao(StudentDao stuDao){
this.stuDao = stuDao;
}
2)使用数据源来管理数据库连接
driverClasss=com.mysql.jdbc.Driver
jdbcUrl=jdbc:mysql://localhost:3306/fortask?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull
username=root
password=20110814Gl
#定义初始连接数
initialSize=0
#定义最大连接数
maxActive=20
#定义最大空闲
maxIdle=20
#定义最小空闲
minIdle=1
#定义最长等待时间
maxWait=60000
<!--配置数据源-->
<context:property-placeholder location="jdbc.properties"/>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${driverClasss}"/>
<property name="url" value="${jdbcUrl}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
<!-- 初始化连接大小 -->
<property name="initialSize" value="${initialSize}"></property>
<!-- 连接池最大数量 -->
<property name="maxActive" value="${maxActive}"></property>
<!-- 连接池最大空闲 -->
<property name="maxIdle" value="${maxIdle}"></property>
<!-- 连接池最小空闲 -->
<property name="minIdle" value="${minIdle}"></property>
<!-- 获取连接最大等待时间 -->
<property name="maxWait" value="${maxWait}"></property>
</bean>
3)使用注解配置controller
@Controller
@RequestMapping(value = "/student")
public class StuController {
//注入业务层的Bean
@Autowired
private StuService stuservice;
}
并在spring配置文件中配置annotation类型的映射器和适配器来完成对controller类中的@RequestMapping标注方法的调用
<!--配置annotation类型的处理映射器-->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
<!--配置annotation类型的处理器适配器-->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>
4)因为需要向前端页面实时传递DB中全部在学弟子人数和各个职业方向的学员数量,在dao层定义了对数据库的原子操作,
public void insertStu (Student stu) {
String sql = "INSERT INTO student VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, )"; //每个?占位符接受一个参数
Object[ ] params = new Object[ ] {stu.getId(), stu.getName(), stu.getProfession(), stu.getProfe_num(), stu.getStudied_time(), stu.getGraduated(), stu.getEmploied(), stu.getCreate_time(), stu.getUpdate_time()}; //定义填充占位符的参数数组
jdbcTemplate.update(sql, params);
}
public void deleteStu (long id) {
String sql = "DELETE FROM student WHERE id = ?";
jdbcTemplate.update(sql, id);
}
public void updateStu (Student stu) {
String sql = "UPDATE student SET name = ?, profession = ?, profe_num = ?, studied_time = ?, graduated = ?, emploied = ?, create_time = ?, update_time = ?";
Object[ ] params = new Object[ ] { stu.getName(), stu.getProfession(), stu.getProfe_num(), stu.getStudied_time(), stu.getGraduated(), stu.getEmploied(), stu.getCreate_time(), stu.getUpdate_time(), stu.getId()}; //定义填充占位符的参数数组
jdbcTemplate.update(sql, params);
}
public int getStuNum( String profession) {
Student stu = new Student();
String sql = "SELECT COUNT (*) FROM student WHERE profession = ?";
Object[ ] params = new Object[ ] { stu.setProfession(profession)};
return jdbcTemplate.update(sql, params);
}
public List<Student> getAllStu(){
String sql = "SELECT * FROM student WHERE graduated = 'F'";
return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Student.class));
}
对于最后一个getAllStu方法,作用查询表中所有记录,然后传递给业务层组件,通过controller把统计出的所有记录数量返回给jsp页面。这里使用Spring JDBC代替传统JDBC,但是对Spring JDBC常用的接口不熟悉,在网上找查询表全部记录的一些实例,但是IDEA一直报错,最后找到这个jdbcTemplate.queryForList(params),计划明天试试。
2.目前controller组件以及tiles标签的JSP页面还没有写出来。
二、明日计划
因为本人需要参加考试,接下来请假三天。
三、遇到的问题
1、jdbcTemplate提供的update、execute和query等方法掌握不多,之前遇到的 一些用法欠缺整理归纳;
2、@Controller、@RequestMapping @RequestParam和@ModelAttribute等注解的用法没有掌握,在controller类中,向前端页面传值和重定向用到的常见方法也不是很熟悉。
四、收获
以上。
评论