SSM框架完整开发流程

----------------第一阶段--------------

1.数据库建模
2.生成sql语句
3.在mysq客户端使用命令方式执行sql脚本,生成数据库

4.允许远程访问mysql

GRANT ALL PRIVILEGES ON *.* TO 'root'@'%'IDENTIFIED BY 'mypassword' WITH GRANT OPTION; 

如果是固定ip就这么写

 grant all privileges on *.* to 'root'@'192.168.41.100'identified by '123456' with grant option;


//推送设置到内存或重启服务器也行

mysql>FLUSH PRIVILEGES 

5.使用mybatis提供框架,执行代码,修改一些数据
1)数据库连接的信息:驱动类、连接地址、用户名、密码

<jdbcConnection driverClass="com.mysql.jdbc.Driver"        connectionURL="jdbc:mysql://192.168.41.100:3306/testtest" userId="root"    password="123456">
</jdbcConnection>

2)targetPackage="com.softjx.model"

3)把表名与类名对应

<table schema="testtest" tableName="school" domainObjectName="School"></table>

<table schema="testtest" tableName="student" domainObjectName="Student"></table>

6.运行GeneratorSqlmap

-------------------------第二阶段--------------------------

1.新建一个WEB项目
2.导入ssm所需要所有jar包,把所有jar放到WEB-INF/lib目录下
3.把mybatis生成dao,model复制到当前项目
4.建com.softjx.service,com.softjx.service.impl
5.需要相关的配置文件(mybatisconfig.xml,dbconfig.properties,applicationContext.xml,log4j.properties)
1 )修改dbconfig.properties文件,ip,数据库,用户名,密码
2)修改applicationContext.xml中的包名,目录名

6.在com.softjx.service包中写接口,在com.softjx.service.impl写接口的实现。
注意:com.softjx.service.impl写接口的实现,
@Service("studentService")
@Transactional

类中注入
@Autowired
private StudentMapper studentMapper;

7.单元测试:
要注意mysql数据库中主键要自增,这一步要我们去mysql中设置。
要注意:studentService = (StudentService) context.getBean("studentService");
这个"studentService"是从@Service("studentService")

-------------------------第三阶段--------------------------
1.web.xml配置文件要修改。
2.在类路径下src目录新建springmvc.xml
3.建com.softjx.action包
4.在com.softjx.action编写action
要注意:
1)@Controller
   @RequestMapping("/student")

2)
   @RequestMapping("/studentAddInput")

类中注入
@Autowired
private StudentService studentService;

5.在WEB-INF目录下建views文件夹
6.在views文件夹建jsp文件,这个jsp文件名是作为action中方法 return的值。
7.添加页面的jsp,要注意控件的属性名是javabean的属性名。
8.在WEB-INF目录下jsp是不能在页面上直接访问,要通过程序访问。

-----------------------第四阶段---------------------------
1.dao层(mybatis)
2.service层(spring)
3.单元测试
4.action(springmvc)
5.jsp html,htm (界面)


1 )查询所有数据

2)单条件查询

3)多条件查询

4)分页查询
-------------------
5) 单条件分页查询

6)多添加分页查询
-------------------
7) 修改(先查询单个实体)
 
8)删除(先查询单个实体)

-----------------------第五阶段---------------------------
1)文件上传          
a.要导入commons-fileupload-1.2.1.jar,commons-io-2.0.jar这两个包

b. <!-- 配置 MultipartResolver 上传文件-->        

      <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="defaultEncoding" value="UTF-8"></property>
        <property name="maxUploadSize" value="10240000"></property>    
      </bean>    

c.在WEB-INF同一层目录中建一个upload目录

d.上传文件的jsp界面

 <form action="student/checkFileUpload" method="POST" enctype="multipart/form-data">
        文件: <input type="file" name="file"/>
                描述: <input type="text" name="desc"/>
        <input type="submit" value="上传数据"/>
   </form>


e.编写上传文件的action

2)文件下载
a.在WEB-INF同一层目录中建一个upload目录
b.下载的url:

<a href="student/fileDownload?filename=hibernate1.jpg">通过 文件流 的方式下载文件hibernate1.jpg</a>


c.编写下载文件的action

-----------------------第六阶段---------------------------
1.把数据库中数据导出到excel
1)导入包poi-3.2-FINAL-20081019.jar
2)在action中写代码
3)注意excel表格中的各种格式

-----------------------第七阶段(两个表查询,有关系)---------------------------
一.根据条件查询
1).掌握多表查询

select a.*,b.* from student a,school b where a.t_sid=b.t_id

select a.t_id,a.t_name,a.t_age,a.t_enterdate,b.t_name as t_name1 from student a,school b where a.t_sid=b.t_id

select a.t_id,a.t_name,a.t_age,a.t_enterdate,b.t_name as t_name1 from student a LEFT JOIN school b on a.t_sid=b.t_id

2).设计实体类中要关联类,在Student类中有School类的引用。

    private School school;
    public School getSchool() {
        return school;
    }

    public void setSchool(School school) {
        this.school = school;
    }

3)在StudentMapper.xml编写sql语句

 <!-- 返回多表查询的字段名 -->
  <resultMap id="WithSchoolResultMap" type="com.softjx.model.Student" >
    <id column="t_id" property="tId" jdbcType="INTEGER" />
    <result column="t_name" property="tName" jdbcType="VARCHAR" />
    <result column="t_age" property="tAge" jdbcType="INTEGER" />
    <result column="t_enterdate" property="tEnterdate" jdbcType="TIMESTAMP" />
    <result column="t_sid" property="tSid" jdbcType="INTEGER" />
     <!-- 指定联合查询出的学校字段-->
    <association property="school" javaType="com.softjx.model.School">
        <id column="t_id1" property="tId"/>
        <result column="t_name1" property="tName"/>
    </association>
  </resultMap>


  
  

  <!-- 多表查询的字段名 -->
   <sql id="Student_School_Column_List">
      a.t_id, a.t_name, a.t_age, a.t_enterdate, a.t_sid,b.t_id as t_id1,b.t_name as t_name1
   </sql>


  
  

  <!-- 查询学生同时带学校信息 -->
  <select id="selectByExampleWithSchool" resultMap="WithSchoolResultMap">
       select
        <if test="distinct">
          distinct
        </if>
        <include refid="Student_School_Column_List" />
        FROM student a
        left join school b on a.t_sid=b.t_id
        <if test="_parameter != null">
          <include refid="Example_Where_Clause" />
        </if>
        <if test="orderByClause != null">
          order by ${orderByClause}
        </if>
  </select>
  

4).在dao中编写一个接口

 //多表查询
    List<Student> selectByExampleWithSchool(StudentExample example);

5).在service层编写接口与接口的实现

//多表查询
     public List<Student> getStudentWithSchool(StudentExample studentExample);

     public List<Student> getStudentWithSchool(StudentExample studentExample) {
        
        return studentMapper.selectByExampleWithSchool(studentExample);
    }


    
6)编写单元测试多表查询。

二.根据主键查询两个表
1) .

select a.t_id,a.t_name,a.t_age,a.t_enterdate,b.t_name as t_name1 from student a LEFT JOIN school b on a.t_sid=b.t_id  where a.t_id=3

2).设计实体类中要关联类,在Student类中有School类的引用。

    private School school;
    public School getSchool() {
        return school;
    }

    public void setSchool(School school) {
        this.school = school;
    }

3).在StudentMapper.xml编写sql语句

 <!-- 返回多表查询的字段名 -->
  <resultMap id="WithSchoolResultMap" type="com.softjx.model.Student" >
    <id column="t_id" property="tId" jdbcType="INTEGER" />
    <result column="t_name" property="tName" jdbcType="VARCHAR" />
    <result column="t_age" property="tAge" jdbcType="INTEGER" />
    <result column="t_enterdate" property="tEnterdate" jdbcType="TIMESTAMP" />
    <result column="t_sid" property="tSid" jdbcType="INTEGER" />
     <!-- 指定联合查询出的学校字段-->
    <association property="school" javaType="com.softjx.model.School">
        <id column="t_id1" property="tId"/>
        <result column="t_name1" property="tName"/>
    </association>
  </resultMap>
  


  

  <!-- 多表查询的字段名 -->
   <sql id="Student_School_Column_List">
      a.t_id, a.t_name, a.t_age, a.t_enterdate, a.t_sid,b.t_id as t_id1,b.t_name as t_name1
   </sql>
  


  

  <!-- 主键查询多表数据 -->
   <select id="selectByPrimaryKeyWithSchool" resultMap="WithSchoolResultMap">
       select 
    <include refid="Student_School_Column_List" />
    FROM student a
        left join school b on a.t_sid=b.t_id
    where a.t_id = #{tId,jdbcType=INTEGER}
  </select>

4).在dao中编写一个接口

Student selectByPrimaryKeyWithSchool(Integer tId);

5)在service层编写接口与接口的实现

        public Student selectByPrimaryKeyWithSchool(Integer tId);

        public Student selectByPrimaryKeyWithSchool(Integer tId) {
        
            return studentMapper.selectByPrimaryKeyWithSchool(tId);
        }

6)编写单元测试主键查询多个表中的数据。
------------------------------第八阶段springmvc使用拦截器---------------------
1.编写一个类继承HandlerInterceptor接口
在这个方法中实现业务

public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
            Object arg2) throws Exception {
        System.out.println("第一个拦截器中的 preHandle方法被调用");

                        //1).从sesion中获取用户对象
            //2).用当前用户所拥有的菜单url是否包含当前请求
        }

2.配置springmvc的配置文件,添加拦截器

 

<mvc:interceptors>
     
         <!--局部拦截器配置, 配置拦截器不作用的路径 ,要先配置<mvc:mapping path="/**" /> ,否则报错,不能少这个-->
        <mvc:interceptor>
        <mvc:mapping path="/**" />  
        <mvc:exclude-mapping path="/login/**"/>
        <bean class="com.softjx.interceptor.ActionMethodInterceptor"></bean>
</mvc:interceptor>    


     
3.登录时要保存当前用户对象到session

--------------------第九阶段 mybatis使用三表或者更多表查询--------------------
一.不使用数据库中的多表查询(使用用户多,十万,百万,千万,亿)
思想:
1.查询学生表
2.根据学生所有学校的id,查询对应学校名称
3.根据学校的区域id查询区域名
4.用查询的数据组装一个vo(view object)(javabean), 这个javabean只是显示在界面上的。
5.多个vo组装ArrayList中
6.显示ArrayList中的数据

如何做:
1.要把以前写的两个表的关联xml复制到一个地方,使用生成框架,生成后添加方法到dao中和xml中
2.新建一个javabean ,是一个vo
3.在service中编写一个方法,返回一个vo的集合类型。(这个里面要仔细看下)
4.测试一下这个方法。

二.使用数据库中的多表查询(使用用户一般几百,几千,几万)

SELECT  a.*,b.*,c.*  from student a,school b,areatable c where a.t_sid=b.t_id and b.t_area_id=c.t_id
SELECT  a.t_id,a.t_name,a.t_age,a.t_enterdate,b.t_name as t_name1,c.area  from student a,school b,areatable c where a.t_sid=b.t_id and b.t_area_id=c.t_id

思想:
1.使用sql语句
2.在StudentMapper接口中写方法
3.要在StudentMapper.xml中写sql语句实现
  1)编写一个<resultMap>
  2)编写接口中方法的实现

  <resultMap id="BaseSqlResultMap" type="com.softjx.vo.StudentSchoolAreaVo" >
    <id column="t_id" property="id" jdbcType="BIGINT" />
    <result column="t_name" property="name" jdbcType="VARCHAR" />
    <result column="t_age" property="age" jdbcType="TINYINT" />
    <result column="t_enterdate" property="enterDate" jdbcType="DATE" />
    <result column="t_name1" property="schoolName" jdbcType="VARCHAR" />
    <result column="area" property="areaName" jdbcType="VARCHAR" />
  </resultMap>

  

   <select id="getStudentSchoolAreaSqlVo" resultMap="BaseSqlResultMap">
       SELECT  a.t_id,a.t_name,a.t_age,a.t_enterdate,b.t_name as t_name1,c.area  from student a,school b,areatable c 
       where a.t_sid=b.t_id and b.t_area_id=c.t_id
  </select>

  4.在service中编写接口方法与实现。


--------------------第十阶段 使用jquery中的ajax技术--------------------
1.需要一个jquery的类库.js  放到static目录下,要注意拦截器放行。
2.jsp中导入js类库。

<script type="text/javascript" src="static/js/jquery-1.8.3.js"></script>


 jquery的环境搭建完毕。

3.在springmvc中开发一个业务方法。

@ResponseBody//返回的数据是json数据
@RequestMapping("/studentName")
public List<Student> getStudentName(Map<String, Object> map, Student student) {
    StudentExample studentExample = new StudentExample();
    Criteria criteria = studentExample.createCriteria();

    if (student.gettName() != null && !student.gettName().trim().equals(""))
        criteria.andTNameEqualTo(student.gettName());

    List<Student> students = studentService.getStudents(studentExample);
    return students;
}

注意:1)返回值是对象类型或者是基本类型,2)@ResponseBody//返回的数据是json数据

4.在jsp页面上控制我们操作的构件,使用jquery的选择器,选中此对象。

5.编写ajax代码

<script type="text/javascript">

$(document).ready(function(){
    $("#nameid").blur(function(){      
        //真实场景下的ajax调用        
        $.ajax(
        {                
            url: "student/studentName1",
            cache: false,
            type: "GET",
            dataType:"json",
            async: true,
            data: {tName:$("#nameid").val()},
            success: function(msg){ 
                //业务代码,改变页面的数据             
                //alert(msg);
                if (msg==true){
                   $("#namewrongid").text("此用户名存在!");
                   $("#nameid").focus();
                } else {
                       $("#namewrongid").text("此用户名不存在!");
                       $("#ageid").focus();
                }
             },
             error:function(errordata){
                 alert("wrong!!"+errordata);
             }
        });
    });                
});

</script> 

 

posted @ 2019-12-24 11:52  我好高冷  阅读(1905)  评论(0编辑  收藏  举报