2020-11-12 10 0
编程杂谈

说明对JDBC进行简单的封装,方便使用完整代码:https://pan.bigdataboy.cn/#/s/nMC0数据库数据创表DROPTABLEIFEXISTSuser;CREATETABLEuser(idBIGINT(20)NOTNULLauto_incrementCOMMENT'主键ID',nameVARCHAR(30)NULLDEFAULTNULLCOMMENT'姓名',ageINT(11)NULLDEFAULTNULLCOMMENT'年龄',emailVARCHAR(50)NULLDEFAULTNULLCOMMENT'邮箱',PRIMARYKEY(id));加入数据INSERTINTOuser(id,name,age,email)VALUES(1,'Jone',18,'test1@bigdataboy.com'),(2,'Jack',20,'test2@bigdataboy.com'),(3,'Tom',28,'test3@bigdataboy.com'),(4,'Sandy',21,'test4@bigdataboy.com'),(5,'Billie',24,'test5@bigdataboy.com');构建目录工具类连接工具类,封装连接对象,方便获取连接packagecn.bigdataboy.util;importjava.sql.PreparedStatement;importjava.sql.Connection;importjava.sql.DriverManager;importjava.sql.ResultSet;importjava.sql.SQLException;publicclassDBConnectionUtil{//创建连接参数privatestaticStringdriver="com.mysql.jdbc.Driver";privatestaticStringurl="jdbc:mysql://localhost:3306/jdbctest?characterEncoding=utf8";privatestaticStringusername="";privatestaticStringpassword="";//获取连接对象publicstaticConnectiongetConnection(){Connectionconn=null;//获取连接try{//加载驱动Class.forName(driver);conn=DriverManager.getConnection(url,username,password);}catch(SQLExceptionthrowables){throwables.printStackTrace();}catch(ClassNotFoundExceptione){e.printStackTrace();}returnconn;}//关闭全部publicstaticvoidcloseAll(ResultSetrs,PreparedStatementps,Connectionconn){if(rs!=null){try{rs.close();}catch(SQLExceptionthrowables){throwables.printStackTrace();}};if(ps!=null){try{ps.close();}catch(SQLExceptionthrowables){throwables.printStackTrace();}};if(conn!=null){try{conn.close();}catch(SQLExceptionthrowables){throwables.printStackTrace();}}}}测试连接工具类publicstaticvoidmain(String[]args){Connectionconnection=getConnection();System.out.println(connection);}用户实体类用户实体类的字段需要与数据表字段对应packagecn.bigdataboy.entity;publicclassUser{privateintid;privateStringname;privateintage;privateStringemail;publicintgetId(){returnid;}publicvoidsetId(intid){this.id=id;}publicStringgetName(){returnname;}publicvoidsetName(Stringname){this.name=name;}publicintgetAge(){returnage;}publicvoidsetAge(intage){this.age=age;}publicStringgetEmail(){returnemail;}publicvoidsetEmail(Stringemail){this.email=email;}}定义dao层接口dao层主要功能是提供对数据库数据的操作类packagecn.bigdataboy.dao;importcn.bigdataboy.entity.User;publicinterfaceUserDao{//通过id查询用户UsergetUserById(intid);}实现UserDao接口packagecn.bigdataboy.dao.impl;importcn.bigdataboy.dao.UserDao;importcn.bigdataboy.entity.User;importcn.bigdataboy.util.DBConnectionUtil;importjava.sql.Connection;importjava.sql.PreparedStatement;importjava.sql.ResultSet;importjava.sql.SQLException;publicclassUserDaoImplimplementsUserDao{//通过id查询用户publicUsergetUserById(intid){Useruser=null;Connectionconn=null;PreparedStatementps=null;ResultSetrs=null;try{//通过连接工具类获取连接对象conn=DBConnectionUtil.getConnection();Stringsql="select*fromuserwhereid=?";ps=conn.prepareStatement(sql);ps.setInt(1,id);//设置第一个问号的值rs=ps.executeQuery();//执行SQL语句//判断是否返回到值,多个值需要循环使用next()获取if(rs.next()){//设置user值user=newUser();user.setId(rs.getInt("id"));user.setName(rs.getString("name"));user.setAge(rs.getInt("age"));user.setEmail(rs.getString("email"));}}catch(SQLExceptionthrowables){throwables.printStackTrace();}finally{//关闭全部DBConnectionUtil.closeAll(rs,ps,conn);}returnuser;}}测试实现的getUserById()方法packagecn.bigdataboy.dao;importcn.bigdataboy.dao.impl.UserDaoImpl;importcn.bigdataboy.entity.User;importorg.junit.Test;publicclassTestUserDaoImpl{@TestpublicvoidTestGetUserById(){intid=1;UserDaoImpluserDao=newUserDaoImpl();Useruser=userDao.getUserById(id);System.out.println(user.getId());System.out.println(user.getName());System.out.println(user.getAge());System.out.println(user.getEmail());System.out.println(user.getClass());}}其他的修改、添加、删除数据都是一样的,完整代码里有

2020-10-17 80 0
编程杂谈

v-text设置标签的文本属性v-text会覆盖原本标签的值标签想要部分设置文本可以使用{{}}方式v-text、{{}}支持逻辑表达式<scriptsrc="js/vue.js"type="text/javascript"charset="utf-8"></script><body><divid="app"><h2>信息:{{message}}</h2><h2>信息:{{message+"!"}}</h2><h2v-text="name">姓名</h2><h2v-text="name+'!'">姓名</h2></div></body><script>varvue=newVue({el:"#app",data:{message:"大数据男孩",name:"Bigdataboy",}})</script>v-html设置元素的innerHTML,data对象里的HTML结构会解析成标签,v-text只会解析成文本<scriptsrc="js/vue.js"type="text/javascript"charset="utf-8"></script><body><divid="app"><pv-html="content"></p></div></body><script>varvue=newVue({el:"#app",data:{content:"<h2>大数据男孩</h2>"}})</script>v-on为元素绑定事件完整格式:v-on:事件="方法名"缩写:@事件="方法名"绑定的方法定义在methods属性里面相关事件:click点击、dblclick双击、mouseenter鼠标移动…<body><divid="app"><inputtype="button"value="v-on指令"v-on:click="fun"><inputtype="button"value="v-on缩写"@click="fun"><pv-text="message"@click="changeMessage"></p></div></body><script>varvue=newVue({el:"#app",data:{message:"大数据男孩",},methods:{fun:function(){alert("事件绑定")},changeMessage:function(){this.message+="真帅"},},})</script>v-on补充传参事件绑定方法写成函数调用形式,可以传入自定义参数<scriptsrc="js/vue.js"type="text/javascript"charset="utf-8"></script><body><divid="app"><inputtype="button"value="按钮"@click="doBtn('老铁','666666')"/></div></body><script>varvue=newVue({el:"#app",methods:{doBtn:function(p1,p2){console.log(p1,p2)}}})</script>修饰符事件的后面跟上.修饰符可以对事件进行限制更多访问:https://cn.vuejs.org/v2/api/#v-on<scriptsrc="js/vue.js"type="text/javascript"charset="utf-8"></script><body><divid="app"><inputtype="text"@keyup.enter="doEnter('回车被点击')"/></div></body><script>varvue=newVue({el:"#app",methods:{doEnter:function(p){console.log(p)}}})</script>v-show用于显示&隐藏标签支持条件表达式本质是控制display:none;<scriptsrc="js/vue.js"type="text/javascript"charset="utf-8"></script><body><divid="app"><!--布尔值--><imgv-show="true"src="./img/1.jpg"style="width:10%;height:10%;"/><!--变量控制--><imgv-show="isShow"src="./img/1.jpg"style="width:10%;height:10%;"/><!--条件表达式--><imgv-show="age>18"src="./img/1.jpg"style="width:10%;height:10%;"/></div></body><script>varvue=newVue({el:"#app",data:{isShow:true,age:17},})</script>v-if用于显示&隐藏标签支持条件表达式本质是判定为false时把标签移除v-if相对于v-show,更消耗性能,因为v-if需要操作DOM树<body><divid="app"><inputtype="button"@click="toggleIsShow"value="切换v-if"/><p></p><!--变量控制--><imgv-if="isShow"src="./img/1.jpg"style="width:10%;height:10%;"/><!--条件表达式--><imgv-if="age>18"src="./img/1.jpg"style="width:10%;height:10%;"/></div></body><script>varvue=newVue({el:"#app",data:{isShow:true,age:17},methods:{toggleIsShow:function(){this.isShow=!this.isShow}}})</script>v-bind为元素绑定属性完整写法:v-bind:属性缩写::属性如果设置的class属性,建议使用对象方式判断是否添加类名<scriptsrc="js/vue.js"type="text/javascript"charset="utf-8"></script><body><divid="app"><!--绑定src属性(完整写法)--><imgv-bind:src="isSrc"style="width:10%;height:10%;"/><br/><!--绑定src属性(缩写写法)--><img:src="isSrc"style="width:10%;height:10%;"/><br/><!--绑定类名(建议对象写法)--><img:src="isSrc":class="{img:isShow,active:isActive}"style="width:10%;height:10%;"/><!--绑定类名(数组写法)--><img:src="isSrc":class="[show,active]"style="width:10%;height:10%;"/></div></body><script>varvue=newVue({el:"#app",data:{isSrc:"./img/1.jpg",isShow:true,isActive:true,show:"show",active:"active",}})</script>v-for根据数组生成列表结构v-for经常与数组搭配使用语法:(item,index)in数组改变数组的内容会同步更新到页面上,是响应式的<scriptsrc="js/vue.js"type="text/javascript"charset="utf-8"></script><body><divid="app"><h2>大数据男孩</h2><ul><liv-for="(item,index)inarr">{{index+1}}{{item}}</li></ul><h2>兴趣</h2><h3v-for="iininterest"v-bind:title="i.name">{{i.name}}</h3></div></body><script>varvue=newVue({el:"#app",data:{arr:["帅气","没钱","可爱","有趣","想了解吗?"],interest:[{"name":"摄影"},{"name":"网络"},{"name":"哲学"},]}})</script>v-model便捷设置和获取表单元素的值绑定的数据会和表单元素的值相关联绑定的数据<—->表单元素的值<scriptsrc="js/vue.js"type="text/javascript"charset="utf-8"></script><body><divid="app"><inputtype="text"v-model="message"/><h2v-text="message"></h2></div></body><script>varvue=newVue({el:"#app",data:{message:"",}})</script>

2020-9-28 53 0