package haha;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectKey;
import java.util.List;
public interface UserDao {
@Insert("insert into user(name,age) value(#{name},#{age})")
int insert_withoutPrimaryKey(@Param("name") String name, @Param("age") int age);
int insert_useGeneratedKey(@Param("user") User user);
int insert_selectKey(@Param("user") User user);
@Insert("insert into user(name,age) value(#{user.name},#{user.age})")
@SelectKey(statement = "select last_insert_id()", keyProperty = "user.id", before = false, resultType = int.class)
int insert_selectKeyAnotation(@Param("user") User user);
@Select("select*from user")
List getAll();
}
insert into user set id=#{user.id},name=#{user.name},age=#{user.age}
SELECT last_insert_id()
insert into user(name,age) VALUE (#{user.name},#{user.age})
五、万事俱备,只欠东风
编写一个UserService类测试一下
package haha;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
public class UserService {
public static void main(String[] args) throws IOException {
String resource = "mybatis.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
SqlSessionFactory factory = builder.build(inputStream);
SqlSession session = factory.openSession();
UserDao dao = session.getMapper(UserDao.class);
//使用默认主键
int affectedRows = dao.insert_withoutPrimaryKey("张三", 25);
System.out.println(affectedRows);
//使用useGeneratedKey,将主键注入到user.id中
User u = new User("张三", 17);
affectedRows = dao.insert_useGeneratedKey(u);
System.out.println(affectedRows + " " + u.getId());
//使用selectKey执行在插入之前或之后执行查询语句
affectedRows = dao.insert_selectKey(u);
System.out.println(affectedRows + " " + u.getId());
//使用selectKey注解的方式
affectedRows = dao.insert_selectKeyAnotation(u);
System.out.println(affectedRows + " " + u.getId());
session.commit();
List a = dao.getAll();
a.forEach(System.out::println);
}
}
@Insert("insert into user value(#{id},#{name},#{age})")
@SelectKey(statement="select count(1)from user", keyProperty="id", before=true, resultType=int.class)
int insert(@Param("name") String name, @Param("age") int age);
@Insert("insert into user(name,age) value(#{user.name},#{user.age})")
@SelectKey(statement = "select last_insert_id()", keyProperty = "user.id", before = false, resultType = int.class)
int insert_selectKeyAnotation(@Param("user") User user);
XML的方式
SELECT last_insert_id()
insert into user(name,age) VALUE (#{user.name},#{user.age})
3、在Oracle中使用SelectKey生成主键,通常是“先查询得到主键,再进行插入”
DUAL表是Oracle中的神奇的表 使用序列作为主键
SELECT SEQUENCE_1.NEXTVAL FROM DUAL
insert into SFY_HAZARD_ANALYSIS
HAZARD_ID,
HAZARD_TIME, HAZARD_TITLE, HAZARD_MEASURE, BUILD_ID,
#{hazardId,jdbcType=INTEGER},
#{hazardTime,jdbcType=VARCHAR}, #{hazardTitle,jdbcType=VARCHAR}, #{hazardMeasure,jdbcType=VARCHAR}, #{buildId,jdbcType= INTEGER},
lt;/insert>
使用GUID作为主键
select SYS_GUID() as userId from DUAL
insert into T_USER(userId,userName,birthday,sex,address) values (#{userId},#{userName},#{birthday},#{sex},#{address})
4、使用useGeneratedKeys
insert into spares(spares_id,spares_name,
spares_type_id,spares_spec)
values(#{id},#{name},#{typeId},#{spec})
利用JavaScript进行对象排序,根据用户的年龄排序展示
<script>
var bob={
name;bob,
age:30
}
var peter={
name;peter,
age:30
}
var amy={
name;amy,
age:24
}
var mike={
name;mike,
age:29
}
var john={
FLP
One famous theory in distributed computing, known as FLP after the authors Fischer, Lynch, and Patterson, proved that in a distributed system with asynchronous communication and process crashes,
每一行命令都是用分号(;)作为结束
对于MySQL,第一件你必须牢记的是它的每一行命令都是用分号(;)作为结束的,但当一行MySQL被插入在PHP代码中时,最好把后面的分号省略掉,例如:
mysql_query("INSERT INTO tablename(first_name,last_name)VALUES('$first_name',$last_name')");
题目链接:zoj 3820 Building Fire Stations
题目大意:给定一棵树,选取两个建立加油站,问说所有点距离加油站距离的最大值的最小值是多少,并且任意输出一种建立加油站的方式。
解题思路:二分距离判断,判断函数的复杂度是o(n),这样的复杂度应该是o(nlogn),即使常数系数偏大,但是居然跑了4.5s,也是醉了。 判断函数里面做了3次bfs,但是每次bfs节点最多