发表于: 2019-12-01 23:41:30

1 1029


今天完成的事情:


搭建了ssm框架


我的StudentDao   和   StudentMapper文件  都是一样的CRUD接口


我用的mapper  没有用dao   因为我配置文件是用的mapper映射


studentmapper接口

@Repository
public interface StudentMapper {

Student selectStudentId(long id);

   List<Student> selectStudentName(String name);

   List<Student> selectStudent();

   Integer insertStudent(Student student);

   boolean deleteStudentId(int id);

   boolean updateStudent(Student student);

}


student  省略set get


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:context="http://www.springframework.org/schema/context"
      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/cache
      http://www.springframework.org/schema/cache/spring-cache.xsd">

   <!--扫描dao包下的注解-->
   <context:component-scan base-package="mapper">
   </context:component-scan>
   <!-- 引入配置文件 -->
   
   <!--数据源配置-->
   <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
       <property name="driverClassName">
           <value>com.mysql.cj.jdbc.Driver</value>
       </property>
       <property name="url">
           <value>jdbc:mysql://localhost:3306/student?serverTimezone=UTC</value>
       </property>
       <property name="username">
           <value>root</value>
       </property>
       <property name="password">
           <value>451976</value>
       </property>
   </bean>
   
   <!--mybatisspring的整合,不需要mybatis自己的配置映射文件-->
   <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
       <!--配置连接数据库数据源-->
       <property name="dataSource" ref="dataSource"></property>

       <!-- mybatisxml文件和mappere接口不在相同包下时,需要用mapperLocations属性指定xml文件的路径。
       *是个通配符,代表所有的文件,**代表所有目录下 -->
       <property name="mapperLocations" value="classpath*:StudentMapper.xml"></property>
   </bean>

   <!--    bean工厂-->
   <bean id="mapperFactoryBean"  class="org.mybatis.spring.mapper.MapperFactoryBean">
       <!--        mapperInterface指定接口-->
       <!--        将接口和映射文件整合在一起-->
       <property name="mapperInterface" value="mapper.StudentMapper"></property>
       <!--        配置sqlSessionFactory-->
       <property name="sqlSessionFactory" ref="sqlSessionFactory"></property>

   </bean>
   
   <bean id="student" class="model.Student"></bean>
</beans>


springmvc.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: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-4.1.xsd
     http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">

   <!-- 开启注解扫描   这里扫描的是我的控制层包   用来扫描@Controller注解 -->
   <context:component-scan base-package="controller" />

   <!-- 创建一个视图解析器(InternalResourceViewResolver)对象 -->
   <!--    找到web工程/WEB-INF/pages文件夹  且文件结尾为jsp的文件作为映射-->
   <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
       <property name="prefix" value="/WEB-INF/pages/"/>
       <property name="suffix" value=".jsp"/>
   </bean>

   <!-- 开启SpringMVC框架注解的支持   用来扫描@requestmapping注解 -->
   <!--    添加此配置  可以自动加载处理映射器(requestMappinghandleMapping) 和处理适配器(requestMappinghandleAdapter)-->
   <!--    总结一句话:用它开启注解驱动 -->
   <mvc:annotation-driven />

   <!--    此配置可访问静态资源 如js css jpg-->
   <mvc:default-servlet-handler/>
   <!--    若配置数据库事务,需开启注解事务  -->
   <!--    <tx:annotation-driven transation-manager="transactionManager" />-->

</beans>



studentmapper.xml  映射文件

<?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属性 区分不同表中查询的sql语句的名字冲突-->
<!--    比如 A findById   B findById名 字冲突不识别,所以给不同的表的数据源添加一个前缀,对应的是namespace属性-->
<mapper namespace="mapper.StudentMapper">
   <!--    id给当前的sql语句起一个识别id,最好和接口一致
           parameterType  限制传入参数类型-->
   <!--    resultType结果集封装的类型,只有查询才能用到
           #{id}占位符里面的名字任意-->

   <!--    //查找单个   传入类型必须为包装型Integer-->
   <select id="selectStudentId"  parameterType="java.lang.Integer"  resultType="model.Student">
     select * from bj where id = #{id}
</select>

   <!--    按姓名查找,传入参数为STRING  -->
   <select id="selectStudentName"  parameterType="java.lang.String"  resultType="model.Student">
     select * from bj where name = #{name}
</select>
   <!--    //查找全部   输出类型必须为user定义类-->
   <select id ="selectStudent"    resultType="model.Student">
     select * from bj
</select>

   <!--    //插入单个   传入类型必须为包装型user定义类-->
   <insert id="insertStudent"    parameterType="model.Student">
    insert into bj (id,name,qq,type,time,stunum,daily,wish,senior) values (#{id},#{name},#{qq},#{type},#{time},#{stunum},#{daily},#{wish},#{senior})
</insert>

   <!--    //删除单个   传入类型必须为包装型user定义类-->
   <delete id="deleteStudentId"   parameterType="model.Student" >
   delete from bj where id = #{id}
</delete>

   <!--    //更改单个    传入类型必须为包装型user定义类-->
   <update id="updateStudent"  parameterType="model.Student" >
   update bj set name = #{name} where id = #{id}
</update>

   <!--            MyMapper.xml名字可以更改,位置任意-->
   <!--            建议命名为:类名+Mapper.xml-->
</mapper>


web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
        http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
        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>

<!--配置前端控制器-->
   <servlet>
       <servlet-name>dispatcherServlet</servlet-name>
       <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--       全局初始化参数-->
       <init-param>
           <param-name>contextConfigLocation</param-name>
           <param-value>classpath:springmvc.xml</param-value>
       </init-param>
<!--   第一次发请求时就创建servlet类    然后servlet初始化参数加载springmvc.xml  -->
<!--   配置 servlet 的对象的创建时间点:应用加载时创建。  取值只能是非 0 正整数,表示启动顺序  -->
       <load-on-startup>1</load-on-startup>
   </servlet>
<!--拦截请求-->
<!--    1. /* 拦截所有 jsp js png .css 真的全拦截 建议不使用-->
<!--    2. *.action *.do 拦截以do action结尾的请求 肯定能使用 ERP-->
<!--    3. / 拦截所有 (不包括jsp) (包含.js .png.css) 强烈建议使用-->
   <servlet-mapping>
       <servlet-name>dispatcherServlet</servlet-name>
       <url-pattern>/</url-pattern>
   </servlet-mapping>

<!--    解决中文乱码的问题-->
<filter>
   <filter-name>characterEncodingFilter</filter-name>
   <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
   <param-name>encoding</param-name>
   <param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
   <filter-name>characterEncodingFilter</filter-name>
   <url-pattern>/*</url-pattern>
</filter-mapping>

   </web-app>


测试了下  controller

 @Controller
   @RequestMapping(value="student")
public class CrudController {

Logger logger = Logger.getLogger(CrudController.class);
//
   @Autowired
   StudentMapper studentMapper;

       @RequestMapping(path = "/test1")
public String postform(Model model) {
List<Student> list = studentMapper.selectStudent();
           logger.info(list);
           model.addAttribute("list",list);
           return "getid";

       }


       @RequestMapping(value = "/test2")
public String HelloWorld() {
System.out.println("HelloWorld");
           return "getid";

       }
}


index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>请选择需要进行的操作</title>
</head>
<body>

请选择需要进行的操作:

<br><a href ="student/test1" method= "get"> test1 </a></br>

<br><a href ="student/test2" method= "get"> test2 </a></br>

</body>
</html>


getid.jsp

<%@ page language="java" contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"% >
<html>
<head>
<title>根据ID查询用户</title>
</head>
<body>
这是结果
<body>
</html>



第一个


第二个


尝试了下没解决

明天再试


明天计划的事情:

完善ssm框架



返回列表 返回列表
评论

    分享到