框架篇(一):优化版

一、环境搭建

1.tomat:Apache Tomcat® - Welcome!

2.JDK:Java Downloads | Oracle 中国

3.小皮(mysql):小皮面板(phpstudy) - 让天下没有难配的服务器环境! (xp.cn)

4.SQLyong:SQLyog Ultimate - Powerful MySQL Development and Administration | Webyog

5.源码及数据库:链接:https://pan.baidu.com/s/1I7RanXbV_S6yqLWrWjqMnA 
提取码:4567

二、声明

此文章为上一篇优化版

三、项目基础搭建及jar包导入

1.建立dao层,实体类pojo,测试类test,工具类utils

2.resources配置文件包

3.lib下导入此次所需jar包

jar包上篇内有分享

四:代码编写

1.utils工具类

mybatisUtils:

package cn.lexed.utils;

import java.io.InputStream;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

/** 
 * @ClassName: MybatisUtils.java 
 * @author        
 * @Date       2019年5月21日 上午10:58:06  
 */
public class MybatisUtils {
	
	      private static SqlSessionFactory factory;
	      
	      static{  //在静态代码块里,factory只会被创建一次
	    	  try{
	    		 InputStream is=Resources.getResourceAsStream("config.xml");
	    		 factory=new SqlSessionFactoryBuilder().build(is);
	    	  }catch(Exception e){
	    		  e.printStackTrace();
	    	  }
	    	  
	      }
	      
	      //获取SqlSession
	      public static SqlSession createSqlSession(){
	    	  
	    	  return  factory.openSession(true); //true为自动提交   
	      }
	      
	      
	      
	      public static void closeSqlSession(SqlSession sqlSession){
	      	  if(null!=sqlSession){
	    		  sqlSession.close();
	    	  }
	      }
}

2.resources

1)db.properties

username=root
password=root
url=jdbc:mysql://localhost:3306/mydb
driver=com.mysql.jdbc.Driver

2)config.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "mybatis-3-config.dtd" >

<configuration>
	<!-- 引入外部文件 -->
	<properties resource="db.properties"></properties>

	<!-- 起别名 -->
	<typeAliases>
		<package name="cn.lexed.pojo"/>
	</typeAliases>
	
	<environments default="dev">
		<environment id="dev">
			<transactionManager type="JDBC">
			</transactionManager>
			
			<dataSource type="POOLED">
				<property name="username" value="${username}"/>
				<property name="password" value="${password}"/>
				<property name="url" value="${url}"/>
				<property name="driver" value="${driver}"/>
			</dataSource>
			
		</environment>
	
		
	</environments>
	
	<!-- 引入sql映射文件 -->
	<mappers>
		<package name="cn.lexed.dao"/>
		<!-- <mapper class="cn.lexed"/> -->
	</mappers>

</configuration>

3.pojo(实体类)

package cn.lexed.pojo;

public class Client {

	// 房间号
    private Integer id;
    // 房间类型
    private String room;
    // 价格
    private Integer price;
    // 描述信息
    private String description;
    // 状态:0:未入住  1:入住
    private Integer status;
    
	public Client() {
		super();
	}

	public Client(Integer id, String room, Integer price, String description, Integer status) {
		super();
		this.id = id;
		this.room = room;
		this.price = price;
		this.description = description;
		this.status = status;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getRoom() {
		return room;
	}

	public void setRoom(String room) {
		this.room = room;
	}

	public Integer getPrice() {
		return price;
	}

	public void setPrice(Integer price) {
		this.price = price;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public Integer getStatus() {
		return status;
	}

	public void setStatus(Integer status) {
		this.status = status;
	}

	@Override
	public String toString() {
		return "Client [id=" + id + ", room=" + room + ", price=" + price + ", description=" + description + ", status="
				+ status + "]";
	}
    
    
    
	
}

4.dao层

 1.ClientMapper接口

package cn.lexed.dao;

import java.util.List;

import cn.lexed.pojo.Client;

public interface ClientMapper {

	//根据id查询
	Client selectC(int id);
	
	//查询所有
	List<Client> selectAll();
	
	//添加
	int insertC(Client c);
	
	//删除
	int deleteC(int id);
	
	//修改
	int updateC(Client c);
}

2.ClientMapper.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "mybatis-3-mapper.dtd" >
<mapper namespace="cn.lexed.dao.ClientMapper">

	<!-- 根据id查询 -->
	<select id="selectC" resultType="Client" parameterType="int">
		select * from client where id=#{id}
	</select>
	
	<!-- 查询所有 -->
	<select id="selectAll" resultType="Client">
		select * from client
	</select>
	
	<!-- 增加 -->
	<insert id="insertC" parameterType="Client">
		insert into client(id,room,price,description,status) values(#{id},#{room},#{price},#{description},#{status})
	</insert>
	
	<!-- 删除 -->
	<delete id="deleteC" parameterType="int">
		delete from client where id=#{id}
	</delete>
	
	<!-- 修改 -->
	<update id="updateC" parameterType="Client">
		update Client set room=#{room},price=#{price},description=#{description},status=#{status} where id=#{id}
	</update>
	
</mapper>

5.测试类

package cn.lexed.test;

import static org.junit.Assert.*;

import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import cn.lexed.dao.ClientMapper;
import cn.lexed.pojo.Client;
import cn.lexed.utils.MybatisUtils;

public class ClientTest {

	@Test//增
	public void test() {
		//1.获取SqlSession对象
		SqlSession s=MybatisUtils.createSqlSession();
		//2.调用SqlSession对象操作数据库
		Client cli=new Client(1022,"标间",888,"全景",0);
		s.getMapper(ClientMapper.class).insertC(cli);
		//3.关闭资源
		MybatisUtils.closeSqlSession(s);
	}
	
	@Test//删
	public void test2() {
		//1.获取SqlSession对象
		SqlSession s=MybatisUtils.createSqlSession();
		//2.调用SqlSession对象操作数据库
		s.getMapper(ClientMapper.class).deleteC(1022);
		//3.关闭资源
		MybatisUtils.closeSqlSession(s);
	}
	
	@Test//改
	public void test3() {
		//1.获取SqlSession对象
		SqlSession s=MybatisUtils.createSqlSession();
		//2.调用SqlSession对象操作数据库
		Client cli=new Client(1012,"标间",888,"全景",0);
		s.getMapper(ClientMapper.class).updateC(cli);
		//3.关闭资源
		MybatisUtils.closeSqlSession(s);
	}
	
	@Test//根据id查询
	public void test4() {
		//1.获取SqlSession对象
		SqlSession s=MybatisUtils.createSqlSession();
		//2.调用SqlSession对象操作数据库
		Client cl=s.getMapper(ClientMapper.class).selectC(1012);
		System.out.println(cl);
		//3.关闭资源
		MybatisUtils.closeSqlSession(s);
	}
	
	@Test//查询所有
	public void test5() {
		//1.获取SqlSession对象
		SqlSession s=MybatisUtils.createSqlSession();
		//2.调用SqlSession对象操作数据库
		List<Client> list=s.getMapper(ClientMapper.class).selectAll();
		System.out.println(list);
		//3.关闭资源
		MybatisUtils.closeSqlSession(s);
	}
	

}

 

完成!!

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值