带条件查询
mapper文件的内容:
<select id="getSelectElectron" resultType="electron"> select * from electron where date > #{date} </select>
select的实现:
public void getSelectElectron() throws Exception { Date date =new SimpleDateFormat("yyyy-MM-dd").parse("2015-12-31"); List<electron> getalluser = sqlSession.getMapper(ElectronDao.class).getSelectElectron(date); for (electron user: getalluser) { System.out.println(user.getType()); } }
增加操作
mapper文件的内容:
<insert id="InsElectronDao" useGeneratedKeys="true" keyProperty="id" parameterType="electron">
insert into electron (id,type,price,date) values (#{id},#{type},#{price},#{date})
</insert>
insert的实现:
//添加操作 public void InsElectronDao() throws Exception { BigDecimal decimal =new BigDecimal("2356.23"); Date date =new SimpleDateFormat("yyyy-MM-dd").parse("2016-10-25"); electron elect = new electron(5,"CDMA-2", decimal,date); Integer integer = sqlSession.getMapper(ElectronDao.class).InsElectronDao(elect); System.out.println("id:"+elect.getId()); if (integer>0){ sqlSession.commit(); System.out.println("添加成功!"); }else{ System.out.println("添加失败!"); } }
修改操作
mapper文件的内容:
<update id="UpdateElectronDao" parameterType="electron"> update electron set type=#{type},price=#{price},date=#{date} where id=#{id} </update>
update的实现:
//修改操作 public void UpdateElectronDao() throws Exception { BigDecimal decimal =new BigDecimal("2356.23"); Date date =new SimpleDateFormat("yyyy-MM-dd").parse("2016-10-25"); electron elect = new electron(2,"CDMA-2", decimal,date); Integer integer = sqlSession.getMapper(ElectronDao.class).UpdateElectronDao(elect); if (integer>0){ sqlSession.commit(); System.out.println("修改成功!"); }else{ System.out.println("修改失败!"); } }
删除的操作:
mapper文件的内容:
<delete id="DelElectronDao"> delete from electron where id=#{id} </delete>
delete的实现:
//删除操作 public void DelElectronDao() throws Exception { Integer integer = sqlSession.getMapper(ElectronDao.class).DelElectronDao(4); if (integer>0){ sqlSession.commit(); System.out.println("删除成功!"); }else{ System.out.println("删除失败!"); } }
log4j:
1.pom文件下添加依赖
<dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.14</version> </dependency>
2.log4j.properties放入正确的位置
没有目录需要新建目录,并在项目中标记成resource
3.log4j.properties 配置
#log4j.appender.Stdout=org.apache.log4j.ConsoleAppender #log4j.appender.Stdout.layout=org.apache.log4j.PatternLayout #log4j.appender.Stdout.layout.conversionPattern=%-5p - %-26.26c{1} - %m # #log4j.rootLogger=INFO,Stdout # #log4j.logger.org.apache.wicket=INFO #log4j.logger.org.apache.wicket.protocol.http.HttpSessionStore=INFO #log4j.logger.org.apache.wicket.version=INFO #log4j.logger.org.apache.wicket.RequestCycle=INFO ### 设置### log4j.rootLogger = debug,stdout,D,E ### 输出信息到控制抬 ### log4j.appender.stdout = org.apache.log4j.ConsoleAppender log4j.appender.stdout.Target = System.out log4j.appender.stdout.layout = org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern = [%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n ### 输出DEBUG 级别以上的日志到=E://logs/error.log ### log4j.appender.D = org.apache.log4j.DailyRollingFileAppender log4j.appender.D.File = D://logs/debug.log log4j.appender.D.Append = true log4j.appender.D.Threshold = DEBUG log4j.appender.D.layout = org.apache.log4j.PatternLayout log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n ### 输出ERROR 级别以上的日志到=E://logs/error.log ### log4j.appender.E = org.apache.log4j.DailyRollingFileAppender log4j.appender.E.File =D://logs/error.log log4j.appender.E.Append = true log4j.appender.E.Threshold = ERROR log4j.appender.E.layout = org.apache.log4j.PatternLayout log4j.appender.E.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ]
4.在mybatis-config.xml文件中填写