发表于: 2018-03-27 20:12:28
1 523
今天完成的事情:
springMVC和mybatis整合
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- 把标记了@Controller注解的类转换为bean -->
<context:annotation-config/>
<context:component-scan base-package="com.controller" />
<!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->
<mvc:annotation-driven/>
<mvc:default-servlet-handler/>
<!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
<?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"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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">
<context:annotation-config />
<!--<mvc:annotation-driven/>-->
<context:component-scan base-package="com"/>
<context:property-placeholder location="classpath:db.properties"/>
<!--配置druid连接池-->
<bean id="dataSource" class="${dataSource}" destroy-method="close" >
<property name="username" value="${jdbc.username}"/>
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!--以下是给mybatis的配置,数据库对话工厂,mapper映射-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 指定数据源 -->
<property name="dataSource" ref="dataSource" />
<!-- 指定需要别名的类所在的包名 ,或者在mybatis中配置-->
<!--<property name="typeAliasesPackage" value="com.DAO"/>-->
<!-- 扫描所有xml文件,或者在mybatis中配置 -->
<property name="mapperLocations" value="classpath:com/DAO/*.xml"/>
</bean>
<!-- DAO接口所在包名,Spring会自动查找其下的类 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.DAO" />
</bean>
</beans>
package com.service;
/**
* @author: Arike
* @program: mvcMapper
* @description: springMVC, mybatis整合
* @create: 2018/3/27 下午1:54
*/
import com.POJO.Student;
public interface StudentService {
//根据id查询用户信息
public Student findUserById(int id) throws Exception;
//根据用户名模糊查询
// public List<User> findUserByName(String name) throws Exception;
//添加用户信息
// public void insertUser(User user) throws Exception;
//删除用户信息
// public void deleteUser(int id) throws Exception;
//更新用户信息
// public void updateUser(User user) throws Exception;
}
package com.POJO;
public class Student {
private Integer ID;
private String create_at;
private String update_at;
private String name;
private String dailyLink;
private int QQ;
private String onlineNumber;
private String mail;
private int phone;
private String enrollmentTime;
private String professionType;
private String brotherName;
private String promise;
@Override
public String toString() {
return "student{" +
"id=" + ID +
", name='" + name + '\'' +
'}';
}
public Integer getID() {
return ID;
}
public void setID(Integer ID) {
this.ID = ID;
}
public String getCreate_at() {
return create_at;
}
public void setCreate_at(String create_at) {
this.create_at = create_at;
}
public String getUpdate_at() {
return update_at;
}
public void setUpdate_at(String update_at) {
this.update_at = update_at;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDailyLink() {
return dailyLink;
}
public void setDailyLink(String dailyLink) {
this.dailyLink = dailyLink;
}
public int getQQ() {
return QQ;
}
public void setQQ(int QQ) {
this.QQ = QQ;
}
public String getOnlineNumber() {
return onlineNumber;
}
public void setOnlineNumber(String onlineNumber) {
this.onlineNumber = onlineNumber;
}
public String getMail() {
return mail;
}
public void setMail(String mail) {
this.mail = mail;
}
public int getPhone() {
return phone;
}
public void setPhone(int phone) {
this.phone = phone;
}
public String getEnrollmentTime() {
return enrollmentTime;
}
public void setEnrollmentTime(String enrollmentTime) {
this.enrollmentTime = enrollmentTime;
}
public String getProfessionType() {
return professionType;
}
public void setProfessionType(String professionType) {
this.professionType = professionType;
}
public String getBrotherName() {
return brotherName;
}
public void setBrotherName(String brotherName) {
this.brotherName = brotherName;
}
public String getPromise() {
return promise;
}
public void setPromise(String promise) {
this.promise = promise;
}
}
<?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.DAO.StudentMapper">
<select id="findUserById" parameterType="int" resultType="com.POJO.Student">
select * from student where ID = #{id}
</select>
<!--<select id="findUserByname" parameterType="java.lang.String" resultType="com.Student">-->
<!--select * from student where username like '%${value}%'-->
<!--</select>-->
<!--<insert id="insertUser" parameterType="com.Student">-->
<!--insert into user(username,birthday,sex,address) values(#{username},#{birthday},#{sex},#{address})-->
<!--<selectKey keyProperty="id" order="AFTER" resultType="java.lang.Integer">-->
<!--select last_insert_id()-->
<!--</selectKey>-->
<!--</insert>-->
<!--<delete id="deleteUser" parameterType="java.lang.Integer">-->
<!--delete from student where id=#{id}-->
<!--</delete>-->
<!--<update id="updateUser" parameterType="com.Student">-->
<!--update user set name=#{username} where ID=#{id}-->
<!--</update>-->
</mapper>
package com.DAO;
import com.POJO.Student;
public interface StudentMapper {
//根据id查询用户信息
public Student findUserById(int id) throws Exception;
//根据用户名模糊查询
// public List<User> findUserByName(String name) throws Exception;
//添加用户信息
// public void insertUser(User user) throws Exception;
//删除用户信息
// public void deleteUser(int id) throws Exception;
//更新用户信息
// public void updateUser(User user) throws Exception;
}
package com.controller;
import com.DAO.StudentMapper;
import com.POJO.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
@Controller
@RequestMapping("/user")
public class mvcController {
Student student;
@Autowired
StudentMapper studentMapper;
@RequestMapping("/searchbyid")
public String jumptosearchid( ) throws IOException {
return "searchbyid";
}
/*
* 根据ID查询信息
*
* */
@RequestMapping(value = "/list",method = RequestMethod.GET)
public String select( Integer ID, Model model) throws Exception {
student = studentMapper.findUserById(ID);
model.addAttribute("student", student);
return "userlist";
}
}
明天计划的事情:
用整合后的程序进行数据库的增删改查操作
对比总结springMVC,mybatis和spring,mybatis整合的区别
遇到的问题:
收获:(通过今天的学习,学到了什么知识)
评论