----------------第一階段--------------
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>
