Module: Mybatis::MapperXMLBuilder
- Included in:
- Builder
- Defined in:
- lib/mybatis/builder/mapper_xml_builder.rb
Instance Method Summary collapse
- #build_mapper_xml(workspace, context) ⇒ Object
- #get_all_column(context) ⇒ Object
- #get_class_path(context) ⇒ Object
- #get_mapper_column(field_name) ⇒ Object
- #get_mapper_xml_path(workspace, context) ⇒ Object
- #get_table_name(context) ⇒ Object
- #get_update_values_column(context) ⇒ Object
Instance Method Details
#build_mapper_xml(workspace, context) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/mybatis/builder/mapper_xml_builder.rb', line 6 def build_mapper_xml(workspace,context) #实体类存放目录 mapper_path = get_mapper_xml_path workspace,context FileUtils.makedirs mapper_path unless File.directory? mapper_path file_path = "#{mapper_path}#{context.po_name}Mapper.xml" #实体类对应文件 file = File.new file_path ,"w" file.puts "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>" file.puts "<!DOCTYPE mapper PUBLIC \"-//mybatis.org//DTD Mapper 3.0//EN\"" file.puts " \"http://mybatis.org/dtd/mybatis-3-mapper.dtd\">" file.puts file.puts "<mapper namespace=\"#{self.get_class_path context}Mapper\">" file.puts " <resultMap id=\"BaseResultMap\" type=\"#{self.get_class_path context}\" >" context.attributes.each_with_index do |attr,index| file.puts " <result column=\"#{attr.column_name}\" property=\"#{attr.field_name}\" />" end file.puts " </resultMap>" file.puts file.puts " <insert id=\"insert\" parameterType=\"#{self.get_class_path context} \">" file.puts " insert into #{self.get_table_name context} (" file.puts " #{self.get_all_column context}" file.puts ' )' file.puts ' values(' context.attributes.each_with_index do |attr,index| result = self.get_mapper_column attr.field_name if index != context.attributes.size - 1 result << ',' end file.puts " #{result}" end file.puts ' )' file.puts ' </insert>' file.puts file.puts ' <delete id="delete">' file.puts " delete from #{self.get_table_name context} where id = \#{id}" file.puts ' </delete>' file.puts file.puts " <update id=\"update\" parameterType=\"#{self.get_class_path context}\">" file.puts " update #{self.get_table_name context}" file.puts ' set' context.attributes.each_with_index do |attr,index| result = self.get_mapper_column attr.field_name if index != context.attributes.size - 1 result << ',' end file.puts " #{attr.field_name} = #{result}" end file.puts " where id = \#{id}" file.puts ' </update>' file.puts file.puts " <select id=\"select\" resultType=\"#{self.get_class_path context}\" resultMap=\"BaseResultMap\">" file.puts " select * from #{self.get_table_name context} where id = \#{id}" file.puts ' </select>' file.puts '</mapper>' file.close puts "create file: #{file_path}" end |
#get_all_column(context) ⇒ Object
84 85 86 87 88 89 90 91 |
# File 'lib/mybatis/builder/mapper_xml_builder.rb', line 84 def get_all_column(context) result = '' context.attributes.each_with_index do |attr| result << ',' unless result.end_with? ',' result << attr.column_name end result[1,result.size] end |
#get_class_path(context) ⇒ Object
75 76 77 78 |
# File 'lib/mybatis/builder/mapper_xml_builder.rb', line 75 def get_class_path(context) return "#{context.package}.#{context.po_name}" if context.package "#{context.po_name}" end |
#get_mapper_column(field_name) ⇒ Object
93 94 95 96 97 |
# File 'lib/mybatis/builder/mapper_xml_builder.rb', line 93 def get_mapper_column(field_name) result = "\#{" result << "#{field_name}" result << '}' end |
#get_mapper_xml_path(workspace, context) ⇒ Object
67 68 69 70 71 72 73 |
# File 'lib/mybatis/builder/mapper_xml_builder.rb', line 67 def get_mapper_xml_path(workspace,context) mapper_path = workspace mapper_path << '/' unless mapper_path.end_with? '/' mapper_path << context.package.gsub(/\./,'/') if context.package mapper_path << '/' unless mapper_path.end_with? '/' mapper_path << 'mapper/' end |
#get_table_name(context) ⇒ Object
80 81 82 |
# File 'lib/mybatis/builder/mapper_xml_builder.rb', line 80 def get_table_name(context) "t_#{context.po_name.downcase_first.replace_upcase_to_underline}" end |
#get_update_values_column(context) ⇒ Object
99 100 101 102 103 104 105 106 |
# File 'lib/mybatis/builder/mapper_xml_builder.rb', line 99 def get_update_values_column(context) result = '' context.attributes.each_with_index do |attr| result << ',\n' if result.end_with? ',\n' result << "#{attr.field_name} = " << '#{' << attr.field_name + '}' end result end |